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:
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";
?>
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.
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
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...
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?
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());
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
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')";
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:
<?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>";
?>
<?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");
?>
Bookmarks