My $_SESSION variables are only getting the first letter of each index in my array

I am working on a class project and I need some help with my PHP. I am trying to return a products table then store the information regarding the products into an array. I then use the array to store the info into some $_SESSION variables. I use those sessions to populate some tables.

Right now sessions are returning blanks.

Let me know if there is a better way to do this that actually works.

session_start();

//DB login and info here

$dbc = mysqli_connect($host, $user, $password, $db)
    or die('Unable to connect to Database. Process aborted');

$query = "SELECT * FROM product";

$result = mysqli_query($dbc, $query)
    or die(mysqli_error($dbc));

// close dbc
mysqli_close($dbc);

if (mysqli_num_rows($result) == 0)
{
    echo "ERROR: PRODUCTS NOT LOADED";
    exit;
}

$row = mysqli_fetch_array($result);
$id [] = $row['Product_PK'];
$name [] = $row['Name'];
$desc [] = $row['Description'];
$price [] = $row['Price'];
$qty [] = $row['Quantity'];
for ($i = 0; $i < count($id); $i++)
{
    $_SESSION['prod' . $i . '_ID'] = $id[$i];
    $_SESSION['prod' . $i . '_Name'] = $name[$i];
    $_SESSION['prod' . $i . '_Price'] = $price[$i];
    $_SESSION['prod' . $i . '_Desc'] = $desc[$i];
    $_SESSION['prod' . $i . '_Qty'] = $qty[$i];
}

I put the info into tables like this:

<form method="post" action="" class="jcart">
                <fieldset>
                    <input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
                    <input type="hidden" name="my-item-id" value="<?php echo $_SESSION['prod1_ID'];?>" />
                    <input type="hidden" name="my-item-name" value="<?php echo $_SESSION['prod1_Name'];?>" />
                    <input type="hidden" name="my-item-price" value="<?php echo $_SESSION['prod1_Price'];?>" />

                    <ul>
                        <li><strong><?php echo $_SESSION['prod1_Name'];?></strong></li>
                        <li>Price: $<?php echo $_SESSION['prod1_Price'];?></li>
                        <li>
                            <?php echo $_SESSION['prod1_Desc'];?>
                        </li>
                        <li>
                            Available: <?php echo $_SESSION['prod1_Qty'];?>
                        </li>
                            <label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
                        </li>
                    </ul>

                    <input type="submit" name="my-add-button" value="add to cart" class="button" />
                </fieldset>
            </form>

I'm getting pretty frustrated and just want some help. This thing is almost due and I can't waste time trying to learn all the ins and outs of PHP.

EDIT: Sorry if I offended anyone. I've been grinding on this project for about 2 months now and just got to my php. Thank you for any advice you offered or any help you gave.

Answers


Try changing this:

$row = mysqli_fetch_array($result);
$id [] = $row['Product_PK'];
$name [] = $row['Name'];
$desc [] = $row['Description'];
$price [] = $row['Price'];
$qty [] = $row['Quantity'];

To this:

// While $row is an array created by the result of the query...
while ($row = mysqli_fetch_array($result)) {
  // The $id array continutes to have $row['Product_PK'] piled onto it.
  $id[] = $row['Product_PK'];
  // Same with the $name array, etc, etc.
  $name[] = $row['Name'];
  $desc[] = $row['Description'];
  $price[] = $row['Price'];
  $qty[] = $row['Quantity'];
}
// NOW start with your FOR statement. PHP will remember the values assigned during
// the WHILE loop, even though WHILE has ended (closing curly bracket).

You need to put your mysqli_fetch_array into a loop. By default, mysqli_fetch_array(and its predecessor mysql_fetch_array) only retrieves the current row from the object returned from mysqli_query(and mysql_query).

while($row = mysqli_fetch_array($result)){
   array_push($id, $row["Product_PK"];
   array_push($name, $row["Name"];
   array_push($desc, $row["Descripition"];
   array_push($price, $row["Price"];
   array_push($qty, $row["Quantity"];
}

Of course you'll have to define your arrays first so put this in before the while loop above.

$id = $name = $desc = $price = $qty = array();

After the PHP interrupter runs the above, you'll end up with 5 arrays (not 5 regular variables). This is the part where I think the logic you're using is a bit confusing. On your HTML mark-up, you only have one form. So the question is, which "product's" information that was returned from the database pull do you want to insert into the form?

Based on what you have so far, you'll need to wrap PHP markers around your entire form. Below would be an example

<php?
for ($i = 0; $i < count($id); $i++):
    $current_id = $_SESSION['prod' . $i . '_ID'] = $id[$i];
    $current_name = $_SESSION['prod' . $i . '_Name'] = $name[$i];
    $current_price = $_SESSION['prod' . $i . '_Price'] = $price[$i];
    $current_desc = $_SESSION['prod' . $i . '_Desc'] = $desc[$i];
    $current_qty = $_SESSION['prod' . $i . '_Qty'] = $qty[$i];
?>
<form method="post" action="" class="jcart">
                <fieldset>
                    <input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
                    <input type="hidden" name="my-item-id" value="<?php echo $current_id;?>" />
                    <input type="hidden" name="my-item-name" value="<?php echo $current_name; ?>" />
                    <input type="hidden" name="my-item-price" value="<?php echo $current_price; ?>" />

                    <ul>
                        <li><strong><?php echo $current_name;?></strong></li>
                        <li>Price: $<?php echo $current_price;?></li>
                        <li>
                            <?php echo $current_desc;?>
                        </li>
                        <li>
                            Available: <?php echo $current_qty;?>
                        </li>
                            <label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
                        </li>
                    </ul>

                    <input type="submit" name="my-add-button" value="add to cart" class="button" />
                </fieldset>
            </form>
<?php
endfor
?>

Now the form mark-up is wrapped with PHP markers. $i number of forms will be created; one each for every item in your arrays.


Try this

    $i=0
    while(rows = mysqli_fetch_array($result)) {       

    $_SESSION['prod' . $i . '_ID'] = $row['Product_PK'];;
    $_SESSION['prod' . $i . '_Name'] = $row['Name'];
    $_SESSION['prod' . $i . '_Price'] = $row['Price'];
    $_SESSION['prod' . $i . '_Desc'] = $row['Description'];
    $_SESSION['prod' . $i . '_Qty'] = $row['Quantity'];

    $i++;
    }

Need Your Help

Bookmarking with eclipse

visual-studio eclipse keyboard-shortcuts resharper

I really miss the bookmarking feature of Visual Studio + Resharper.

paypal html api problem with displaying email with @?

html paypal

whenever i send a form hidden field to paypal with an email as a field value containing @ eg.