Jump to content

for each loop pull information from database


adrian28uk

Recommended Posts

I am putting together a shopping cart for educational purposes. I decided to redo some of my earlier efforts as looking at the code, 10 lines could be put in to 4. You know what its like.

 

I am sending the values from two drop down menus to an array.

Then I am trying to use a for each loop to extract each number against the database which in turn should output the product option name, however I cannot get this to work. I'm seriously scratching my head here.

 

The array is ready to go, I tested this by using print_r ($product_option);

 

$product_option = $_POST['choice']; 

   

foreach ($product_option as $number)

{

include "dbconn.inc.php";

 

$dbcnx = @mysql_connect("$servername","$username", "$password"); 

if (!$dbcnx) { echo( "" );   

exit();  }

 

//Connect to database

if (! @mysql_select_db("$database") ) { 

echo( "<P>Unable to locate the shopping basket " . "" );   

exit();  }

 

$query = mysql_query("SELECT * FROM products_options WHERE product_number=$number); 

while ( $row = mysql_fetch_array($query) )

{

  echo $row['product_option_name'];

}

 

}

 

Look forward to your response.

Link to comment
Share on other sites

What specific error u get.

And try to remove @ from ur code lines.

It will show u the warning error.

Change this line :

$query = mysql_query("SELECT * FROM products_options WHERE product_number=$number); 

To this:

$query = mysql_query("SELECT * FROM products_options WHERE product_number=$number) or die (mysql_error()); 

 

Link to comment
Share on other sites

Certainly. By the way that error checking is great. Thanks for the tip.

I have updated some variables etc on the code below.

 

Basically $product_choice will equal two numbers taken from the form for example 453,8721

Using these numbers, each one is put in to the foreach loop where in turn queries the database.

 

So for example code 453 will print out Colour Red

code 8721 will print out Size large

 

Like I said before I can create the array, and when I print_r($array) I can see the information. The only problem I am having is that it is not getting any information from the database. If I replace $_POST['choice'] with two static numbers it works, but not when I post it from the form.

 

Many many thanks. Code is below.

 

// Code

$product_choice = $_POST['choice']; 

   

foreach ($product_choice as $numbers)

{

include "dbconn.inc.php";

 

$dbcnx = @mysql_connect("$servername","$username", "$password"); 

if (!$dbcnx) { echo( "" );   

exit();  }

 

//Connect to database

if (! @mysql_select_db("$database") ) { 

echo( "<P>Unable to locate the shopping basket " . "" );   

exit();  }

 

 

$query = mysql_query("SELECT * FROM Products_Options WHERE product_randid=$numbers") or die (mysql_error());

 

while ( $row = mysql_fetch_array($query) )

{

echo $row['product_option_name'];

}

}

Link to comment
Share on other sites

Ok first of all move your db connection code out side of the foreach. You don't want to make a new connection to the database every time you loop through the foreach loop. Just have the query within the loop.

 

New code:

<?php

// we moved all the db connection/selection code outside of the foreach
// don't wont to connect/select database each time we loop through the foreach
include 'dbconn.inc.php';

$dbcnx = mysql_connect($servername, $username, $password) or die('Connection Failed');
mysql_select_db($database) or die('"Unable to locate the shopping basket');

$product_choice = $_POST['choice'];

foreach ($product_choice as $numbers)
{
    // Build the query
   $query = 'SELECT * FROM Products_Options WHERE product_randid=' . $numbers;

   // Run the query we built above
   $result = mysql_query($query, $dbcnx) or die (mysql_error());

    // check that the query returned only one record
    if(mysql_num_rows($result) == 1)
    {
        // one record has been returned, lets get it.
        // no need for a while loop here as we are only getting one record
        // if multiple records are being returned we'd use a while loop
        $row = mysql_fetch_assoc($result);

        // display the returned product
        echo $row['product_option_name'];
    }
}

?>

Link to comment
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.