php - 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="" >
<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" />
</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.
Answer
Solution:
Try changing this:
To this:
Answer
Solution:
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).
Of course you'll have to define your arrays first so put this in before the while loop above.
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
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.
Answer
Solution:
Try this