PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi all, just starting out on my road to php - got php, mysql and IIS installed on my cough cough windows pc, I can connect to the database via php fine, and php is working on the system.
My problem is I am trying to create a product menu pulled from a database - the code is as follows below
- the error which is in the title referrs to the variable $count
It's probably something blaringly obvious I have missed
<?php
// include the header for the homepage located in the file header_home_page.php
include "extras/header_home_page.php"
?>
<body>
<div>
<div id="top_strip">
<div id="logo">
</div>
</div>
<div id="menu">
<h2>Main Menu</h2>
<?php
//include "database/database_connection_string.php"
//Declare Count Variable
$count = 0; //This is used to avoid that "|" is printed before the first menu item
// Get the data into the staging part of the system (not yet available to php
// or MySQL
$query = mysql_query("SELECT * FROM product ORDER BY Product_ID ASC")
or die(mysql_error());
while ($menu_row = mysql_fetch_object($query))
{
if ($count!=0){echo " | ";} // display the | after the menu name
//output the menu name (.$menu_row->Menu.Name.) and on click redirect to product_display.php page and
//pass the variable product_ID to the page (?Product_ID=$menu_row->Product_ID\") then goto a new line (<br>)
echo "<a href=\"product_display.php?Product_ID=$menu_row->Product_ID\">.$menu_row->Menu_Name.</a><br>";
//count = count + 1
$count++;
}
?>
// dont need rest of page code
It was Something to do with including the database connection in the php tags if i verbosely do it in a seperate menu include and include the menu.php file it works fine - if anyone knows why feel free to post Its all learning
<?php
/* connect to the localhost database - It doesnt look like much but this will connect you trust me <br>
use the username webuser and the password secret and make the connection persistant. The @ symbol
forces the function to use the custom error message below itself */
@mysql_pconnect ("localhost","root","#####") // using mysql_pconnect means that php will check to see
// if the database connection exists. If it doesnt then
// it will create one but if it does then it won't worry
// about creating another one and will just use the open
// connection.
or die("Hard luck no such database connection"); // Error message to be displayed if no connection exists
echo 'Genius! Connected to the database'; // this displays regardless but if you get the error message
// something is wrong
// Connect to the correct database
@mysql_select_db("tyrone")
or die("Could not select database!");
//Declare Count Variable
$count = 0; //This is used to avoid that "|" is printed before the first menu item
// Get the data into the staging part of the system (not yet available to php
// or MySQL
$query = mysql_query("SELECT * FROM products ORDER BY Product_ID ASC") or die(mysql_error());
while ($menu_row = mysql_fetch_object($query))
{
if ($count!=0){echo " | ";} // display the | after the menu name
//output the menu name (.$menu_row->Menu.Name.) and on click redirect to product_display.php page and
//pass the variable product_ID to the page (?Product_ID=$menu_row->Product_ID\") then goto a new line (<br>)
echo "<a href=\"product_display.php?Product_ID=$menu_row->Product_ID\">$menu_row->Menu_Description</a><br>";
//count = count + 1
$count++;
}
?>
quite right i had too. lol, ahh now my page just has a menu include to run the menu and in the menu include it has the database include (with the ";") and it works as sweet as candy, thanks guys