calvinschools Posted May 4, 2011 Share Posted May 4, 2011 All i get is a link to view all partners. What I want to see is all the people in the database and how many comments each has. Anyone? Was on last night but still couldn't get it woking <?php $db = mysql_connect('--', '--', '-!'); if (!$db) { die('Could not connect: ' . mysql_error()); } mysql_select_db ('-',$db); $max_items = 5; function displayPartners($all = 0){ global $db, $max_items; if ($all == 0){ $query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC LIMIT $max_items"; }else{ $query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC"; } if (!$db) while ($row = mysql_fetch_assoc($result)){ echo "<TABLE border = \"1\" width=\"300\>\n"; $date = $row['date']; $title = htmlentities ($row['title']); echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n"; echo "<TR><TD>$partners</TD></TR>\n"; $comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)"; echo "</TABLE>\n"; echo "<BR>\n"; } if ($all == 0){ echo "<a href =\"{$_SERVER['PHP_SELF']}?action=all\">View all Partners</a>\n"; } } function displayOneItem($id){ global $db; $query = 'SELECT * FROM partners WHERE id = $id'; $result = mysql_query ($query); if (mysql_num_rows ($result) == 0){ echo 'No partners found '; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border =\"1\" width=\"300\>\n"; $title = htmlentities ($row['title']); echo "<TR><TD><b>$title</b></TD></TR>\n"; echo "<TR><TD>$partners<b>/TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; displayComments($id); } function displayComments($id){ global $db; $query = "SELECT * FROM comment WHERE partners_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HRwidth=\"300\">\n"; while ($row = mysql_fetch_assoc ($result)){ echo "<TABLE border=\"1\"width=\"300\">\n"; $name = htmlentities ($row['name']); echo "<TR><TD><b>by:$name</b></TD></TR>\n"; $comment = strip_tags ($row['comment'],'<a><b><i><u>'); $comment = nl2br ($comment); echo "<TR><TD>$comment</TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; } echo "<HR width=\"300\">"; echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n"; echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n"; echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n"; echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n"; echo "</FORM>\n"; } function addComment($id) { global $db; $query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')"; mysql_query ($query); echo "Comment submitted<BR>\n"; echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n"; } echo "<CENTER>\n"; switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayPartners(1); break; case 'addcomment': addComment($GET['id']); break; default: displayPartners(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/ Share on other sites More sharing options...
silkfire Posted May 4, 2011 Share Posted May 4, 2011 Check if $max_items really is 5. It might be null or 0 without you even knowing. Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210537 Share on other sites More sharing options...
calvinschools Posted May 4, 2011 Author Share Posted May 4, 2011 check where? database or script Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210542 Share on other sites More sharing options...
spiderwell Posted May 4, 2011 Share Posted May 4, 2011 it doesnt look like you are actually executing the loop with all the info to echo out due to the if (!$db) while ($row = mysql_fetch_assoc($result)){ in the display partners function at the point of executing this, you havent created a $result. Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210546 Share on other sites More sharing options...
silkfire Posted May 4, 2011 Share Posted May 4, 2011 Ye you never do myqsl_query() lol no wonder then. You just created the string query, never executed it. =) Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210549 Share on other sites More sharing options...
calvinschools Posted May 4, 2011 Author Share Posted May 4, 2011 hold on. i'm getting confused. what do you mean I didn't do a query? Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210551 Share on other sites More sharing options...
silkfire Posted May 4, 2011 Share Posted May 4, 2011 How come you don't get an error? Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210553 Share on other sites More sharing options...
calvinschools Posted May 4, 2011 Author Share Posted May 4, 2011 I don't know. That's the confusing part. I'm thinking about re-writing the whole thing because this is just not working. I could go about it another way. Does anyone know an easier way to display all the images in a directory in a list along with the number of comments they have, for the user that is? such as: Andrew ( tom(12) then you can click on which one you want to look at. Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210557 Share on other sites More sharing options...
spiderwell Posted May 4, 2011 Share Posted May 4, 2011 How come you don't get an error? because the if (!$db) bit i think, that says if not database connection do the while loop, because there is one it then skips the while statement you should try <?php if ($db) { $result = mysql_query ($query); while ($row = mysql_fetch_assoc($result)){ echo "<TABLE border = \"1\" width=\"300\>\n" //blah blah }//close while }//close if $db if ($all==0) { //blah blah } Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210594 Share on other sites More sharing options...
calvinschools Posted May 4, 2011 Author Share Posted May 4, 2011 Thanks spiderwell but still nothing. I'm just going to write it another way. If any other suggestions I'd appreciate it but I think I need to write some new code. Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210648 Share on other sites More sharing options...
smsmarketeers Posted May 5, 2011 Share Posted May 5, 2011 What is the ultimate goal that you are trying to achieve? The others were right. Your first query which consists of an IF statement to choose between two different queries based on the variable $all never gets executed. if ($all == 0){ $query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC LIMIT $max_items"; } else { $query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC"; } You are missing the mysql_query() function that executes the chosen query. You then move to try looping through the results which do not actually exist because the query was never executed so the result is empty, which should be throwing an error: if (!$db) { while ($row = mysql_fetch_assoc($result)){ // This result set is empty and the mysql_fetch_assoc() function should be throwing an error if display_errors is turned on } } So, what is the overall goal here? Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210665 Share on other sites More sharing options...
calvinschools Posted May 5, 2011 Author Share Posted May 5, 2011 The goal is to just have all the image names in the db display with the number of comments each has: Tom(12) Mike(9)... And you can click which one you want to look at. Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210701 Share on other sites More sharing options...
phpSensei Posted May 5, 2011 Share Posted May 5, 2011 The goal is to just have all the image names in the db display with the number of comments each has: Tom(12) Mike(9)... And you can click which one you want to look at. You are ignoring the solutions provided. Your code is all over the place, the "!" exclamation mark means the opposite of the function result. IF the database does not connect, then DO NOT run the while loop. Use your frickin brackets... and this ($all = 0){ completely useless What is that function even supose to return? Why is your max_item and db globalized? Wheres $result coming from? Some parsing problems here you should fix echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)"; Quote Link to comment https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/#findComment-1210767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.