www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: php link

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    php link

    Hi everyone,

    I am having trouble figuring out where my problem is with what I am trying to do. I am trying to let my users click on a letter of the alphabet (which I have listed) and when my users click on it, I want the next page to list all poems that start with what letter was clicked on. I know the if statement at the bottom of this post is wrong, I am not sure how to fix that - what type of operation to perform.

    Thanks,
    Randy

    Example:
    I click on letter p, and the next page shows:

    P
    Poem that starts with letter p
    Poem that starts with letter p
    Poem that starts with letter p
    Poem that starts with letter p


    So far I have the following code which prints out all the letters A-Z:

    Code:
    <?php
    	print date('F jS, Y');
    	echo "<h3>Browse By Letter</h3>";
    ?>
    
    <div id="navletters">
    <?php
    foreach(range('A', 'Z') as $letter) {
        echo "<a href=\"includes/poemsbyletter.inc.php?id= \">$letter</a><br>\n";
    }
    ?>
    </div><!--navletters-->
    This is the code I am trying to use to list the poems. You can see that my SELECT query isn't complete as I am not too sure what to do with that:

    Code:
    <?php
    
    
    
    $id = $_GET['id'];
    $letter_id = $_GET['letter_id'];
    $title = $_GET['title'];
    $poem = $_GET['poem'];
    
    $dbh = mysql_connect ($db_hostname, $db_username, $db_pass, $db_name) or die ('I am unable to connect to the database because of: ' . mysqli_error());
    
    mysql_select_db("mydatabasename",$dbh);
    
    $query = "SELECT * FROM `poems` WHERE letter_id ORDER BY `poems`.`letter_id` ASC LIMIT 0, 30 \n";
    
    $result = mysql_query($query) or die(mysql_error());
    
    if ($result == 0){
    	echo "<h3>Sorry, there are no poems beginning with the letter $letter_id</h3>\n";
    }
    
    echo "<br>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)/* or die('No records retrieved')*/)
    
    {
      if ($letter_id == $letter_id){
        echo "letter_id";
      }
    }
    echo "$letter_id";
    echo "<br><br>";
    echo "<a href=\"../index.php\" />Back To Home</a>\n";
    
    ?>

  2. #2
    Join Date
    Jul 2009
    Posts
    184
    I think your query would work better like this:

    Code:
    $query = "SELECT * FROM poems WHERE substr(columnName, 1, 1) = " . $letter;
    columnName should be your table column where the poems title is stored, and the substr() function checks the first letter of that column.

    The $letter variable should contain the letter that the user clicked on.

    My mysql syntax could be a little off, but the idea should work alright.

  3. #3
    Join Date
    Mar 2011
    Posts
    67
    Thanks for the help. It appears I am now heading in the correct direction. I tried to trim down all that code I was posting. Here is what I have now:

    This is the link that is clicked on:
    Code:
    <?php
    	print date('F jS, Y');
    	echo "<h3>Browse By Letter</h3>";
    ?>
    
    <?php
    include("includes/connect_to_db.inc.php");
    
    mysql_select_db("mypoemdatabase", $dbh);
    
    $query = "SELECT id,letter_id,title,poem FROM `poems` LIMIT 0, 30 ";
    
    $result = mysql_query($query, $dbh);
    
    $row = mysql_fetch_array($result);
    
    for ($letter = 'A'; $letter < 'Z'; $letter++) {
        echo "<a href=\"includes/poemsbyletter.inc.php?id=$letter\" />$letter</a><br>\n";
    }
    ?>
    And that points to this code that should list the poems that start with letter clicked on:

    Code:
    <?php
    include("includes/connect_to_db.inc.php");
    
    mysql_select_db("mypoemsdatabase",$dbh);
    
    $poemid=$_GET['id'];
    $letter=$_GET['letter_id']; //not sure if this is needed or if it is, what I should be trying to grab.
    
    $query = "SELECT * FROM poems WHERE substr(title, 1, 1) = \" . $letter ORDER BY `poems`.`id` ASC LIMIT 0, 30\n"; 
    
    $result = mysql_query($query, $dbh);
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)or die('No records retrieved'))
        {
       echo "$poemid";
           }
    echo "<br><br>\n";
    echo "<a href=\"../index.php\" />Back To Home</a>\n";
    ?>
    With this code, I am now getting the 'No records retrieved' message.

    thanks!
    Randy

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Do some error checking so MySql can tell you what is wrong with your query:
    PHP Code:
    $result mysql_query($query$dbh);
    if(!
    $result){
       die(
    mysql_error($dbh));


  5. #5
    Join Date
    Mar 2009
    Posts
    452
    also try this
    $query = "SELECT * FROM poems WHERE title like '$letter%';

  6. #6
    Join Date
    Mar 2011
    Posts
    67
    Thank you for the tips. The error messages have been helpful. I have not had any extra like updating my query to WHERE title like '$letter'.

    I have had a bit more luck though so I am still have problems, but I thought I would post my updated code here to see if anyone spots any problems. Thanks-a-bunch in advance for helping a stranger.

    Here is my navigation Include file. I have this on my main site and I am trying to have a user click on a letter and then the next page that will open up with be a page I have called poemsbyletter, where all the poems with that letter (that was clicked) will be listed.

    nav.inc.php

    Code:
    <?php
    $con = mysql_connect("localhost", "login", "pwd") or die('Sorry, could not connect to database server');
    
    mysql_select_db("db", $con) or die('Sorry, could not connect to database');
    
    $query = "SELECT id,letter_id,title,poem FROM poems";
    
    $result = mysql_query($query) or die('Sorry, could not get any poems at this time.');
    
    	while($row = mysql_fetch_array($result, MYSQL_ASSOC)){   	
    		$id=$row['id'];
    		$title=$row['title'];
      		}
    		foreach(range('A', 'Z') as $letter ){
    		echo "<a href=\"testpoemsbyletter.inc.php?id=$id&letter=$letter&title=$title\" />$letter</a><br>\n";
      			}
    ?>
    Here is the poemsbyletter.inc.php code:

    Code:
    <?php
    $id=$_GET['id'];
    $title=$_GET['title'];
    $letter=$_GET['$letter'];
    
    $db_hostname = "localhost"; //Host
    $db_username = "login"; //Username
    $db_pass = "pwd"; //Password
    $db_name = "db"; //Database Name
    
    // connect to database
    
    	$dbh = mysql_connect ($db_hostname, $db_username, $db_pass, $db_name) or die ('I am unable to connect to the database because of: ' . mysql_error());
    	$query = "SELECT * FROM poems WHERE title like '$letter%'"; 
    	$result = mysql_query($query) or die('Sorry, could not get poems at this time ');
    
    	while($row = mysql_fetch_array($result, MYSQL_ASSOC)){   	
    		echo "$id";
    		echo "$title";
    	}
    ?>
    And, my MySQL table has these four column headings: id,letter_id,title,poem

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Are you getting any errors? Unexpected behavior? I have to admit I didn't look to hard at your code since there wasn't anything specific to keep an eye out for...

  8. #8
    Join Date
    Mar 2009
    Posts
    452
    what actual problem/error are you facing ?

  9. #9
    Join Date
    Mar 2011
    Posts
    67
    I apologize for my lack of communication. The problem is that when I click on a letter, example, letter "P". When the code takes me to my next page (poemsbyletter.inc.php), I get the message: Sorry, could not get poems at this time. I would have expected to get something like this:

    Letter P
    Poem starts with letter p listed here
    Poem starts with letter p listed here
    etc..

    That message is part of my code at: $result = mysql_query($query) or die('Sorry, could not get poems at this time ');

    So I figure, I made it that far in the code, what could be the issue that I am not get any data?

    Thank you.

  10. #10
    Join Date
    Mar 2009
    Posts
    452
    that means there is some problem with your query. try the following to find your error message
    $result = mysql_query($query) or die('Sorry, could not get poems at this time '.mysql_error());

  11. #11
    Join Date
    Mar 2011
    Posts
    67
    Thank you, ZABI! Turns out, when I entered my database in mysql_connect, it wasn't connecting to it. Anyway, I wasn't select a db.

    Now, when I click on a letter, I get the following:
    57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57

    Turns out that the number 57 is the total number of poems in the database.

    Also, I am getting this as my url on the next page when I click on a letter:
    testpoemsbyletter.inc.php?id=57&title=&C=C&letter_id=

    Clearly some info isn't being passed, I am just not sure why.
    Last edited by tuxandpucks; 09-12-2012 at 09:02 AM.

  12. #12
    Join Date
    Mar 2011
    Posts
    67
    I almost have solved my issue. One last issue is to figure out how to check the letterid against the first letter of the poem title and if they match, print that poem title.

    I am thinking something like:
    if ($letterid == NotSureWhatToPutHere){
    echo poem title
    }

    I am attaching an image of my database so you may see what I mean. Thanks, Randy

    image.png

  13. #13
    Join Date
    Mar 2009
    Posts
    452
    actually when you use the query i posted earlier it will return all those results have their title starts with that letter. so we dont have to worry about checking it in php,

    $query = "SELECT * FROM poems WHERE title like '$letter%'";
    and
    $query = "SELECT * FROM poems WHERE letterid = lcase('$letter')";

    both of them should work for you

  14. #14
    Join Date
    Mar 2011
    Posts
    67
    You were correct ZABI! Thank you! I am now able to do what I wish with my code, however, I have encountered a new problem. Whenever I click on a letter, any letter, it always returns the same result (the first title in my list). For example, when I click on the letter 'C', it returns the poem title, All Over Town. When I click on the letter 'F', my results are just, All Over Town. Do you happen to know what would cause this?
    Here is my current code:

    Nav.inc.php:
    Code:
    <?php
    	print date('F jS, Y');
    	echo "<h3>Browse By Letter</h3>"
    	
    ?>
    
    <?php	  
    		foreach(range('A', 'Z') as $letter){
    		echo "<a href=\"includes/poemsbyletter.inc.php?letterid=$letter\" />$letter</a><br>";
    		}
    ?>
    and Poemsbyletter.inc.php:

    Code:
    <?php
    	$letterid=$_GET['letterid'];
    
    	echo "$letterid<br>";
    	
    	$db_hostname = "localhost"; //Host
    	$db_username = "username"; //Username
    	$db_pass = "password"; //Password
    	$db_name = "db"; //Database Name
    
    // connect to database
    
    	$dbh = mysql_connect ($db_hostname, $db_username, $db_pass, $db_name) or die ('I am unable to connect to the database because of: ' . mysql_error());
    	mysql_select_db("tuxandp1_poemsdb", $dbh);
    	$query = "SELECT * FROM poems WHERE title LIKE '$letter%'";
    	$result = mysql_query($query) or die('Sorry, could not get poems at this time '.mysql_error());
    	$row = mysql_fetch_array($result, MYSQL_ASSOC);
    	
    	$title=$row['title'];
    	$letterid=$row['letterid'];
    	
    	echo "$title<br>";
    	
    	
    ?>

  15. #15
    Join Date
    Mar 2011
    Posts
    67
    I have solved my issue with the following code:
    Navigation.inc.php script:
    Code:
    <?php
    	print date('F jS, Y');
    	echo "<h3>Browse By Letter</h3>"
    	
    ?>
    
    <?php	  
    		foreach(range('A', 'Z') as $letter){
    		echo "<a href=\"http://www.poemscribe.com/includes/poemsbyletter.inc.php?letterid=$letter&letter=$letter\" />$letter</a><br>";
    		}
    ?>
    Listing poems by letter script:
    Code:
    <?php
    
    	$letterid=$_GET['letterid'];
    	$letter=$_GET['letter'];
    	
    	$db_hostname = "localhost"; //Host
    	$db_username = "username"; //Username
    	$db_pass = "pwd"; //Password
    	$db_name = "db"; //Database Name
    	
    	echo "<h2>\n";
    	echo "$letterid<br>";
    	echo "</h2>\n";
    // connect to database
    
    	$dbh = mysql_connect ($db_hostname, $db_username, $db_pass, $db_name) or die ('I am unable to connect to the database because of: ' . mysql_error());
    	mysql_select_db("tuxandp1_poemsdb", $dbh);
    	$query = ("SELECT * FROM poems WHERE title LIKE '$letter%'");
    	$result = mysql_query($query) or die('Sorry, could not get poems at this time '.mysql_error());
    	
    	while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    	$title=$row['title'];
    	echo "$title<br>";
    	}
    	
    	include("nav.inc.php");
    	
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles