Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012855
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012888
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1012901
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1013168
Share on other sites

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>";

?>

 

Link to comment
https://forums.phpfreaks.com/topic/192172-please-help-looping/#findComment-1013261
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.