Jump to content


Photo

Retrieving data from MySQL


  • Please log in to reply
1 reply to this topic

#1 jimmyelewis

jimmyelewis
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 16 March 2006 - 06:00 PM

Hey I've made a database in mysql which has several different fields one containing the name of the categories i have on the site. I'm trying to code the page so that it does the correct category name this is what i have.

Nav. Bar

if ($results = mysql_query ($query))
    {
        //Retrieves and prints every record
        while ($row = mysql_fetch_array ($results))
        {
            echo '<li><a class="nav" href="' . 'index.php?cat_num=' . $row['cat_num'] . '">' . $row['cat_name'] . '</a></li>';
        }
    } 

Content of Category
if (isset($_GET['cat_num'])) { //makes sure that the code is only executed when the option variable is present
$query = 'SELECT cat_name FROM admin_cat';
$results = mysql_query ($query);
$row = mysql_fetch_array ($results);
    switch ($_GET['cat_num']) {
        case '1':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
        case '2':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
        case '3':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
    }
}
else {
            echo '<div class="title">' . $row['cat_name'] . '</div>';    }

When i click on each link it shows the same category name and i'm not sure how to get it to display a certain field based on another field in the database. Thanks for any help.

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 16 March 2006 - 07:07 PM

Put it in the WHERE clause:

//makes sure that the code is only executed when the option variable is present
if (isset($_GET['cat_num'])) { 
   $query = 'SELECT cat_name FROM admin_cat WHERE cat_num="' . $_GET['cat_num'] . '"';
   $results = mysql_query ($query);
   $row = mysql_fetch_array ($results);
   echo '<div class="title">' . $row['cat_name'] . '</div>';
} else {
   echo '<div class="title">No category selected.</div>';
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users