Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

doing the same thing twice.

Featured Replies

ok this is gonna be really simple, but I am loading a few pages using this code..

 

<?php
$menu_product_id=0;
while($row = mysql_fetch_array($result))
{
	$menu_product_id=$row['ID'];
	echo "<li><a href=\"product.php?id=" . $menu_product_id . "\">". $row['Name'] ."</a></li>"; 

}
$i=0;
$num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows , useful for creating layers or whatever you need for each product
?>

 

i have the SQL connect code at the beginning of the <body> tag and everything is working ok.

so what I would like to do is use the same code to populate the menu. I actually have it working by using the same code in the menu div... but then the code wont work a second time for the links in the main content. i reckon im getting my variables crossed a bit.. but id appreciate some help. thanks.

<?php
       $menu_product_id=0;
       while($row = mysql_fetch_array($result))
       {
               $menu_product_id=$row['ID'];
               echo "<li><a href=\"product.php?id=" . $menu_product_id . "\">". $row['Name'] ."</a></li>"; 

       }
       $i=0;
       $num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows , useful for creating layers or whatever you need for each product
       mysql_data_seek($result, 0);
?>

  • Author

oh! thanks for your reply, but im not quite sure what that should do... :) if you could elaborate that would be sweet.

 

is it placed in the 2nd usage of the chunk of code? so repeat the same code twice, once in the menu, and once in the main content + your line ??

 

thanks

Put

mysql_data_seek($result, 0);

 

in after the first loop, that will reset the mysql pointer to the first record then you can loop through them again.

Rather than looping through it twice would it not make more sense to set the result as a variable e.g. $stuff .= instead of echoing it. Then you can just echo the $stuff as often as you like.

is it not better practice to create an array of the items and then use the array when needed so as to avoid calling the database twice ?

is it not better practice to create an array of the items and then use the array when needed so as to avoid calling the database twice ?

 

I've often wondered this as well. Could it raise memory issues if there were too many results?

I've often wondered this as well. Could it raise memory issues if there were too many results?

 

dunno, if the array was gonna cause memory issues, surely calling the database would too ????

 

but I am not sure which is better, maybe warrants some benchmarking to see

 

 

 

Blofeld is correct, if you need the exact same results then it would be more efficient to store it in a variable (depending on number of results) and echoing it out twice.

 

If not then looping it twice is fine. mysql_data_seek will just reset the internal pointer to the first record so so you can loop through them again, there is no gain in storing it in an additional array. It doesn't re-query the database or anything like that.

 

However if you have APC on your server then you would gain performance by dumping the result into and array and caching the array. I'd highly recommend doing this on something like navigation.

Blofeld is correct, if you need the exact same results then it would be more efficient to store it in a variable (depending on number of results) and echoing it out twice.

 

If not then looping it twice is fine. mysql_data_seek will just reset the internal pointer to the first record so so you can loop through them again, there is no gain in storing it in an additional array. It doesn't re-query the database or anything like that.

 

However if you have APC on your server then you would gain performance by dumping the result into and array and caching the array. I'd highly recommend doing this on something like navigation.

 

Thanks Jock !! always quick with a reply :)good.gif

 

 

 

  • Author

thanks for all the help.

although this keeps on leading up to more and more problems :)

my individual product page is doing the same thing as the index page (as above) was doing. only this time neither the menu list works or the product info. the data does not seem to pass through.

 

basically im just keeping it as simple as possible, connecting to the db and grabbing whatever info i need. thing is this is my first php venture... so im lost for answers most of the time.

ill post my relevant code and please if you have a simpler or more effective way to connect the pages i would be most grateful!!

Thanks guys

 

Index.php
<body>
<?php
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
   die('Could not connect: ' . mysql_error());
 } 
 mysql_select_db("gwines_test") or die(mysql_error());					
 $result = mysql_query("SELECT ID,Name FROM products") 
 or die('Blah ' . mysql_error()); 
				?>
<div id="wrapper">
<div id="content">
<div id="header">     
   <div id="menu">
   <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a>
      <ul>           
      <?php
  	  $product_id=0;
  while($row = mysql_fetch_array($result))
  {
    $product_id=$row['ID'];
    echo "<li><a href=\"product.php?id=" . $product_id . "\">". $row['Name'] ."</a></li>"; 
  }
 $i=0;
 $num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows
 mysql_data_seek($result, 0);
	?>
        </ul>
        </li>

           </div>  

       </div>
       <div id="main">
       <h1>"Wine is a pleasure that transports us from the present to the past" </h1>
       <?php
	$product_id=0;
	while($row = mysql_fetch_array($result))
        {
	  $product_id=$row['ID'];
	  echo "<li><a href=\"product.php?id=" . $product_id . "\">". $row['Name'] ."</a></li>"; 
	}
	$i=0;
	$num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows
?>
</div>

 

 

Product.php
<body>

<?php
 /** connect to the product datatase **/
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
   die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("gwines_test") or die(mysql_error());
 if(is_numeric($_GET['id']))
 $product_id=$_GET['id'];
 else
 $product_id = -1;
 $result = mysql_query("SELECT id, name, producttype, description, winery, imgloc, thumbloc    FROM products WHERE id='" . $product_id . "'") 
 or die('Blah ' . mysql_error());
 $row = mysql_fetch_array($result); 
?>

<div id="wrapper">
<div id="menu">
 <li><a href="#">Products</a>
 <ul>
 <?php
   $product_id=0;
   while($row = mysql_fetch_array($result))
   {			
    $product_id=$row['ID'];
    echo "<li><a href=\"product.php?id=" . $product_id . "\">". $row['Name'] ."</a></li>"; 
  }
  $i=0;			  				
  $num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows 
?>
</ul>
</li>
</div>
<div id="main">
<?php
 echo "<h4>" . $row['name'] . "</h4><br/>"; ?>
 <img src="images/line.png" width="613" height="2" />
 <?php
   echo '<img src="' . $row['imgloc'] . '" id="prod_img" /><br/>'; 
   echo "<h5>" . $row['name'] . "</h5>";				
   echo "<p>Description: " . $row['description'] . "<br/>";
   echo "Winery: " . $row['winery'] . "<br/>";
   echo "<a href=\"index.php\">[Go Back]</a></p>";
?>
</div>

 

thanks again

You seemed to miss of your if else brackets to and also didn't have the ID field in uppercase where it should have been.

 

You code can be shortened a lot like this:

 

Index.php
<body>
<?php

//  *************    You should put this in a separate file and either require() or include() it e.g. "connectDB.php"
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
   die('Could not connect: ' . mysql_error());
 } 
//  ***************** End   ****************************
//and possibly this line as well if you only use one db 
mysql_select_db("gwines_test") or die(mysql_error());


//******  Run this once....					
 $result = mysql_query("SELECT ID,Name FROM products") 
 or die('Blah ' . mysql_error()); 
$product_id=0;
  while($row = mysql_fetch_array($result))
  {
    $product_id=$row['ID'];
    $navList .= "<li><a href=\"product.php?id=" . $product_id . "\">". $row['Name'] ."</a></li>"; 
  }

//*** this next part seems to be unused and can be deleted:
 $i=0;
 $num=mysql_num_rows($result); ////// Finding all the information in the table and counting the rows
 mysql_data_seek($result, 0);
//***end of uselessness  *****
				?>
<div id="wrapper">
<div id="content">
<div id="header">     
   <div id="menu">
   <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a>
      <ul>           
      <?php echo $navList; 	?>
        </ul>
        </li>

           </div>  

       </div>
       <div id="main">
       <h1>"Wine is a pleasure that transports us from the present to the past" </h1>
<ul>
     <?php  
//I added the ul tags too!!
echo $navList; 	?>
</ul>
</div>

 

 

Product.php
<body>

<?php
// get database connect info
 require("connectDB.php");

 if(is_numeric($_GET['id']))
{ 
//since you are calling from the url directly to the sql query, use mysql_real_escape_string() for some security
$product_id= mysql_real_escape_string($_GET['id']);
}
 else{
 $product_id = -1;
}
// I also notice in your index page your 'id' field in the DB is uppercase so change it to that.
 $result = mysql_query("SELECT ID, name, producttype, description, winery, imgloc, thumbloc    FROM products WHERE id='" . $product_id . "'") 
 or die('Blah ' . mysql_error());
$product_id=0;
   while($row = mysql_fetch_array($result))
   {			
    $product_id=$row['ID'];
    $navList .= "<li><a href=\"product.php?id=" . $product_id . "\">". $row['Name'] ."</a></li>"; 
  }

?>

<div id="wrapper">
<div id="menu">
 <li><a href="#">Products</a>
 <ul>
 <?php echo $navList; ?>
</ul>
</li>
</div>
<div id="main">
<?php
 echo "<h4>" . $row['name'] . "</h4><br/>"; ?>
 <img src="images/line.png" width="613" height="2" />
 <?php
   echo '<img src="' . $row['imgloc'] . '" id="prod_img" /><br/>'; 
   echo "<h5>" . $row['name'] . "</h5>";				
   echo "<p>Description: " . $row['description'] . "<br/>";
   echo "Winery: " . $row['winery'] . "<br/>";
   echo "<a href=\"index.php\">[Go Back]</a></p>";
?>
</div>

Edited by Blofeld

  • Author

really appreciate the help. im just going through what you posted now, and it makes alot of sense. the top php brackets on the Product page are causing some sort of error, and leaving me with a blank page. :mellow: any thoughts what it might be???

thanks

Switch error reporting on and it will tell you which line the error is on and what is wrong with it. It's the most useful thing you will ever do!

really appreciate the help. im just going through what you posted now, and it makes alot of sense. the top php brackets on the Product page are causing some sort of error, and leaving me with a blank page. :mellow: any thoughts what it might be???

thanks

like suggested try turning error reporting on by putting this at the top of the page

error_reporting(E_ALL);

  • Author

so i have played with it and come up short..

the error i get seems to be something to do with the array navList. it returns an error on the index page, and on the product page, well nothing seems to be loading there at all. I've uploaded the both pages in a simple form:

 

www.georgianwines.ie

 

error reporting should be turned on. and thanks alot for all the input so far. great discussion. I am looking into a php course as its a bit ridiculous at this stage. but for now, any help is muchly appreciated.

cheers.

The variable navList is not being defined.

 

If it's only being defined conditionally then that condition isn't positive, so therefore it's never actually being defined.

 

Is $navList = *** whatever *** inside a conditional loop? Or a While loop that isn't bringing any results in?

  • Author

i see.. i get what you are saying, but on the index page. the four products are being loaded in no problem, so im assiming

while($row = mysql_fetch_array($result))

is working ok. but maybe this could be the issue on the product page. thanks

i see.. i get what you are saying, but on the index page. the four products are being loaded in no problem, so im assiming

while($row = mysql_fetch_array($result))

is working ok. but maybe this could be the issue on the product page. thanks

 

Try setting $navList manually to something like "THERE IS NO NAV LIST" or whatever. Then run it. If this comes up "THERE IS NO NAV LIST", and isn't being set to something else, then you know it was never originally being set.

 

Edit - just to clarify, set $navList to that silly value BEFORE the loop that sets it in the original code.

Edited by Gibson

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.