georgebates Posted December 12, 2009 Share Posted December 12, 2009 Hello. I am new to PHP and am having some really annoying problems. I have this code that reads entries in a database and then puts them on the page. Each database entry has a category name. When i found the code it just displayed all the entries on the one page but i want it to only display the entries from a certain category name (say all the ones with category name of 'Brian Strong'). So i looked through the code and added in some bits that i though would work. 1) I added the $user with a value of "Brian Strong" $user="Brian Strong"; 2) I added an if statement so that if the category name on that entry was the same as $user then it would display that entry. if ($user=$row['category_name']) { print '<div id="ref_links">'; print "<h3>Painting Name: ".$row['topic_name']."</h3>\n"; print "<p>Price: ".$row['topic_desc']."</p>\n"; print '<br/>'."\n"; print "<a href=".$row['topic_url']."><img src=".$row['topic_url']." /></a>"; print '<p><a href="art_inquiry_form.html">Inquire</a></p>'."\n"; $lastCat = $row['category_name']; // record which category this listing was in so we can test if the cat. changes } 3) I put an else if statement so that if it wasn't the same category then it would print that entry, else if ($user<>$row['category_name']) { print "none"; } But my problem is that it still just prints all the entrys in the database. Anyone's help would be very much appreciated and i would gladly pay someone if they could help to fix this. Thanks Edit: Oh and heres the whole code put together. <?php $myResult = mysql_query('SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id ORDER BY ref_categories_id, ref_links.topic_name', $connectID) or die ("Unable to select from database"); $user="Brian Strong"; while ($row=mysql_fetch_array($myResult, MYSQL_ASSOC)) { $thisCat= $row['category_name']; //print the category heading, but only once for the listings in that catagory if ($lastCat<>$thisCat) { // true first time($lastCat not set), and each time a new category is found print "<h2>".$row['category_name']."</h2>"; // print the next category heading } if ($user=$row['category_name']) { print '<div id="ref_links">'; print "<h3>Painting Name: ".$row['topic_name']."</h3>\n"; print "<p>Price: ".$row['topic_desc']."</p>\n"; print '<br/>'."\n"; print "<a href=".$row['topic_url']."><img src=".$row['topic_url']." /></a>"; print '<p><a href="art_inquiry_form.html">Inquire</a></p>'."\n"; $lastCat = $row['category_name']; // record which category this listing was in so we can test if the cat. changes } else if ($user<>$row['category_name']) { print "none"; } print "</div>\n"; //end of ref_links div } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184929-really-wierd-coding-error-please-help/ Share on other sites More sharing options...
KevinM1 Posted December 12, 2009 Share Posted December 12, 2009 Your if-statement is wrong - = means assignment == means test for equality In your case, your if-statement says "if $row['category_name'] can be assigned to $user, do the following: ..." Quote Link to comment https://forums.phpfreaks.com/topic/184929-really-wierd-coding-error-please-help/#findComment-976216 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 nightslyr beat me to it. you need to change your if statement to == and you should be using != for checking that something is not equal to rather than <>. Quote Link to comment https://forums.phpfreaks.com/topic/184929-really-wierd-coding-error-please-help/#findComment-976217 Share on other sites More sharing options...
georgebates Posted December 12, 2009 Author Share Posted December 12, 2009 It worked. The reason i was using = and <> is because im so used to programming in Visual Basic and stuff. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/184929-really-wierd-coding-error-please-help/#findComment-976222 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.