cr55dan Posted February 15, 2010 Share Posted February 15, 2010 Hi, i'm farely new to php and mysql, to the stage where i don't really have a clue and have a problem with some php&mysql that i've put together. Can someone tell me why the following code only ever returns back to itself, as in the end result only ever shows forum categories from my database? include('../scripts/header.php'); include('../scripts/ses.php'); if (empty($cat)) $cat = ""; // Shows the forum categories if ($cat == "") { $query = " select * from mgb_cat "; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Categories\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Categories</b><br/><br/>"; while($row) { echo "<a href=\"forums.php?ses=$ses&cat=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } // Shows the forums if ($cat != "") { $query = " select * from mgb_forums where cat='$cat'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Forums\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Forums</b><br/><br/>"; while($row) { echo "<a href=\"topics.php?ses=$ses&forum=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } echo "<br/>"; if ( $cat != "" ) echo "<a href=\"forums.php?ses=$ses\" title=\"Menu\">Category Menu</a><br/>"; Any advice would be great Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/ Share on other sites More sharing options...
wildteen88 Posted February 15, 2010 Share Posted February 15, 2010 Delete $row = mysql_fetch_array($result); Now the following line while($row) should be while($row = mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012779 Share on other sites More sharing options...
cr55dan Posted February 15, 2010 Author Share Posted February 15, 2010 ok, i changed that and it still does the same, it just keeps returning the first query result, which is show the forums... As if the if (empty($cat)) $cat = ""; is always empty?? Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012794 Share on other sites More sharing options...
SpankMarvin Posted February 15, 2010 Share Posted February 15, 2010 Does it help if you replace the while clause with foreach($row as $value) { // Stuff in loop } You would then need to change values such as $row['id'] to $value['id']... Worth a try? Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012802 Share on other sites More sharing options...
cr55dan Posted February 15, 2010 Author Share Posted February 15, 2010 Tried that, still only shows what i have in the database. Would it all all help if i gave the sql setup of my database? CREATE TABLE `mgb_cat` ( `id` int(10) NOT NULL auto_increment, `order` varchar(10) NOT NULL default '', `name` varchar(100) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=7 ; # # Dumping data for table `mgb_cat` # INSERT INTO `mgb_cat` (`id`, `order`, `name`) VALUES (1, '1', 'General'), (2, '2', 'Discussion'), (3, '3', 'Music'), (4, '4', 'Technology'), (5, '5', 'Sport'), (6, '6', 'This Site'); And CREATE TABLE `mgb_forums` ( `id` int(10) NOT NULL auto_increment, `cat` varchar(10) NOT NULL default '', `name` varchar(100) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=29 ; # # Dumping data for table `mgb_forums` # INSERT INTO `mgb_forums` (`id`, `cat`, `name`) VALUES (1, '1', 'Everyday'), (2, '1', 'Newbies'), (3, '1', 'International'), (4, '1', 'Fight (no fists)'), (5, '2', 'Discussion'), (6, '2', 'World'), (7, '2', 'Ideas'), (8, '2', 'Rubbish'), (9, '3', 'Bands'), (10, '3', 'Punk'), (11, '3', 'Rock'), (12, '3', 'Metal'), (13, '3', 'Gigs'), (14, '3', 'Other'), (15, '3', 'Musicians'), (16, '4', 'Phones'), (17, '4', 'Games'), (18, '4', 'Computers'), (19, '4', 'Wap&Web'), (20, '5', 'Football'), (21, '5', 'Wrestling'), (22, '5', 'Extreme Sports'), (23, '5', 'Other Sports'), (24, '6', 'Testing Space'), (25, '6', 'Feedback'), (26, '6', 'Suggestions'), (27, '6', 'Help&Support'), (28, '6', 'Problems'); Are the forum tables i am using. Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012855 Share on other sites More sharing options...
cr55dan Posted February 15, 2010 Author Share Posted February 15, 2010 Hi, i'm farely new to php and mysql, to the stage where i don't really have a clue and have a problem with some php&mysql that i've put together. Can someone tell me why the following code only ever returns back to itself, as in the end result only ever shows forum categories from my database? include('../scripts/header.php'); include('../scripts/ses.php'); if (empty($cat)) $cat = ""; // Shows the forum categories if ($cat == "") { $query = " select * from mgb_cat "; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Categories\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Categories</b><br/><br/>"; while($row) { echo "<a href=\"forums.php?ses=$ses&cat=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } // Shows the forums if ($cat != "") { $query = " select * from mgb_forums where cat='$cat'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Forums\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Forums</b><br/><br/>"; while($row) { echo "<a href=\"topics.php?ses=$ses&forum=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } I've made bold the area i think i'm having the probelm, $cat is not defined in the link i have put in bold, how would i do this? Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012888 Share on other sites More sharing options...
SpankMarvin Posted February 16, 2010 Share Posted February 16, 2010 Am I correct in assuming that $cat is defined by some sort of $_GET['cat'] input? If so, try deleting the if(empty... line completely, and placing the if clauses into a function as follows: function get_forums($cat = "") { if (!$cat) { $query = " select * from mgb_cat "; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Categories\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Categories</b><br/><br/>"; while($row) { echo "<a href=\"forums.php?ses=$ses&cat=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } // Shows the forums if ($cat) { $query = " select * from mgb_forums where cat='$cat'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo "<card title=\"Forums\"><p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Forums</b><br/><br/>"; while($row) { echo "<a href=\"topics.php?ses=$ses&forum=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $row = mysql_fetch_array($result); } } } then in your main code, pass $cat through the function: get_forums($cat); This way, if there is truly no $cat value, the function should run as expected. Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012901 Share on other sites More sharing options...
cr55dan Posted February 16, 2010 Author Share Posted February 16, 2010 $cat is not defined using any $get[cat] which i'm assuming is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1013014 Share on other sites More sharing options...
cr55dan Posted February 16, 2010 Author Share Posted February 16, 2010 ok, think i know why its not working, on row: echo "<a href=\"forums.php?ses=$ses&cat=".$row["id"]."\" title=\"".$row["name"]."\">".$row["name"]."</a><br/>"; $cat is not defined, and forums.php is actually the same file, so it does 2 different functions based on if $cat is empty or not. Can anyone see which method would be best to get it from the url above? i used .$row['id']. which seems to show the forum categories correctly, however when the link is clicked, it just loads the forum categories again, because it cant find $cat! Please Help!! Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1013168 Share on other sites More sharing options...
cr55dan Posted February 16, 2010 Author Share Posted February 16, 2010 Just for reference, i made a few adjustments as advised, and used the $_GET function in the page to get my desired result, the way i used $get before did not work, thanks guys for your help!!! <?php include('../scripts/header.php'); include('../scripts/ses.php'); echo "<body>"; $cat = $_GET['cat']; // Show categories if (empty($cat)) { $query = " select * from mgb_cat "; $result = mysql_query($query); echo "<p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Categories</b><br/><br/>"; while($row = mysql_fetch_array($result)) { echo "<a href=\"forums.php?ses=$ses&cat=".$row["id"]."\">".$row["name"]."</a><br/>"; } } elseif (isset($cat)) { $query = " select * from mgb_forums where cat='$cat'"; $result = mysql_query($query); echo "<p align=\"center\"> <img src=\"../logos/forum.gif\" alt=\"forums\" /><br/><b>Forums</b><br/><br/>"; while($row = mysql_fetch_array($result)) { echo "<a href=\"topics.php?ses=$ses&forum=".$row["id"]."\">".$row["name"]."</a><br/>"; } } echo "<br/>"; if (isset($cat)) echo "<a href=\"forums.php?ses=$ses\" title=\"Menu\">Category Menu</a><br/>"; echo "<hr><a href=\"../mainmenu.php?ses=$ses\" title=\"Menu\">Main Menu</a></hr></p></body></html>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1013261 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.