0

Please help me to get this to work, sorry if code is not clean, i am just a beginner in PHP

<?php
$strSQL = "SELECT * FROM ps_product";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");    

    while($prices3 = mysql_fetch_array($objQuery)) {

    $total_price = $priceCalc;
?>  
    <?=$prices3["id_product"];?>=><? echo $total_price; ?>,
    <? }; ?>

The above code gives me the following result (ID => Price):

3=>55, 4=>28, 5=>35,

How can I add the result into an ARRY?

I want to get this done, in order to look like:

        $prices = array(
            3=>55,
            4=>28,
            5=>35,
            ...
        );
        foreach ($prices as $id => $price) {
            $query = "UPDATE ps_product_shop SET price='".$price."' WHERE 
            id_product='".$id."' ";
            mysql_query($query);
        }
5
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Jan 10, 2014 at 15:22
  • 1
    where are you calculating $priceCalc? Commented Jan 10, 2014 at 15:23
  • My oh my... Both question and answers look like contestants for the annual PHP obfuscated code challenge. I understand you are a beginner in PHP, but that does not prevent you from using meaningful names, and even maybe throw in a few comments, or does it? Commented Jan 10, 2014 at 15:31
  • Once your code is working correctly, you could make a visit to Code Review and ask how to improve it. Commented Jan 10, 2014 at 17:32
  • 1
    <? }; ?> <--- next time you write this, you'll be flogged to death with the complete collection of the latest ECMA5 drafts Commented Jan 10, 2014 at 17:36

3 Answers 3

1

Try this

$res_arr = array();
while($prices3 = mysql_fetch_array($objQuery)) {
$res_arr[$prices3["id_product"]] = $priceCalc;
}
print_r($res_arr);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_push like this:

$result = array();
while($prices3 = mysql_fetch_array($objQuery)) {
    $total_price = $priceCalc;
    array_push($result, $total_price);
}
?>  

Comments

0
$i = 0;
$prices = array();
while($prices3 = mysql_fetch_array($objQuery)) {
    $total_price = $priceCalc;
    $prices[$i] = $total_price;
    $i++;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.