Skip navigation
Currently Being Moderated

mySQL/php getting prices from database

Mar 9, 2013 11:27 AM

I have the need to get some prices from a database and display on a page.

 

I can use a repeat region (see below) but have the necessity to reset it before looping through the database again to get the other prices. I need to do this because the prices are scattered all over the page.

 

<?php do { ?>

<?php

if ($row_productPrice['productName'] == "Air Nozzles") {

echo $row_productPrice['productPrice'];

}

?>

<?php } while ($row_productPrice = mysql_fetch_assoc($productPrice)); ?>

 

Then reset the recordset (before looping through the database again):

 

<?php

// reset the recordset after a repeat region

mysql_data_seek($productPrice, 0);

 

// get the first row from the recordset

$row_productPrice = mysql_fetch_assoc($productPrice);

?>

 

OR I can do it by repeating the same mysql query over and over again before echoing the productPrice:

 

<?php

mysql_select_db($database_conProduct, $conProducts);

$query_productPrice = "SELECT productPrice FROM products WHERE productName = 'Air Nozzles'";

$productPrice = mysql_query($query_productPrice, $conProducts) or die(mysql_error());

$row_productPrice = mysql_fetch_assoc($productPrice);

$totalRows_productPrice = mysql_num_rows($productPrice);

?>

 

<?php echo $row_productPrice['productPrice']; ?>

 

 

Both solutions work but seem a bit messy to me is there any alternative solution which would make the code more efficient?

 

Cheers

 

Os.

 
Replies
  • Currently Being Moderated
    Mar 9, 2013 12:11 PM   in reply to osgood_

    Resetting the data pointer is not messy really - I do this often.  But why not just read the recordset once, and set up all your prices during that read?

     

    So, for the initial recordset, pull both product name *and* product price ($rsProducts), then have something like this -

     

    do {

    switch ($row_rsProducts['productName']) {

    case 'Air Nozzle':

         $airNozzle = $row_rsProducts['productPrice'];

         break;

    case 'Hose Clamp':

         $hoseClamp = $row_rsProducts['productPrice'];

         break;

    case 'Venturi Tube':

         $venturiTube = $row_rsProducts['productPrice'];

         break;

    /* continue for each product name, and you will now have a series of variables, with sensible names, each containing the desired price */

         } /* end switch */

    } while ($row_rsProducts = (mysql_fetch_assoc($rsProducts));

     

    Then when you need a price on the page (like for a venturi tube), you can just do -

     

    echo $venturiTube;

     

    Would that work?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2013 6:16 AM   in reply to osgood_

    Switch is like a series of successive if() commands. The difference is that when one of them succeeds, you do not continue to execute the rest. I find a way to use it all the time, and think this is a particularly good application.

     

    Here's something else I often do - I pull a recordset, then dump the entire recordset into an array variable so that I can have free access to the individual elements of the total recordset. It's simple enough to do something like this -

     

    do {

      $variable[] = $row_rsWhatever;

    } while $row_rsWhatever=(mysql_fetch_assoc($rsWhatever));

     

    Then $variable[0] contains the first record, $variable[1] contains the second record, etc.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2013 9:15 AM   in reply to osgood_

    (assuming your recordset also contains the product Manufacturer name)

     

    foreach ($variable as $value) {

    echo ($value['productManufacturer']=='Cadburys'?$value['productManufacture r']:"");

    }

     

    The nice thing is that you don't have to reset the array pointer as it does so automatically when you exit the foreach(). And assuming that the Cadburys record is the very first one in the recordset and that the fields are pulled in the order shown, then $variable[0] would look like what you have.

     

    You can test this yourself by putting this immediately after looping through the recordset to populate the $variable array -

     

    echo "<pre>";exit(print_r($variable));

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2013 9:32 AM   in reply to osgood_

    Good luck, Os! And to your team, too.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2013 8:12 PM   in reply to osgood_

    What you are really looking for is a dictionary object - which is just an associative array - so you can reference the array element by name rather than by index. I'm not a php'er but it would be something like:

     

    do {

     

    $DictProduct[$row_productPrice['productname']] = $row_productPrice['productPrice'];

    }

    while ($row_productPrice = mysql_fetch_assoc($rSproducts));

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points