Jump to content

mysql result into array?


stallprophet

Recommended Posts

I am a little burned out and need a little help on this.  The basic summary: I have a shopping cart script sending info to a processor.

 

I need to send the qty's and item name's in some sort of string via a single variable to the process form.

 

i.e. (3) Hipster Turnips, (6) Butter Milk Baby Brains, (2) Super Freaks

 

Here is the script:

<?php

function writeShoppingCart() {

$cart = $_SESSION['cart'];

if (!$cart) {

return '<p>You have no items in your shopping cart</p>';

} else {

// Parse the cart session variable

$items = explode(',',$cart);

 

$s = (count($items) > 1) ? 's':'';

return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';

}

}

 

function showCart() {

 

include 'db_connect.php';

$cart = $_SESSION['cart'];

if ($cart) {

$items = explode(',',$cart);

$contents = array();

 

foreach ($items as $item) {

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

}

echo "<form action=\"cart.php?action=update\" method=\"post\" id=\"cart\">";

 

foreach ($contents as $id=>$qty) {

$conn2 = db_connect();

if ($conn2) {

$query2 = "SELECT * FROM itmes WHERE id = $id";

 

$result2 = mysql_query($query2,$conn2);

///echo $result;

 

} else {

exit("<font color=\"#000000\">There is an error connecting to database, please cont act an administrator.");

}

///echo $result;

$id2=mysql_result($result2,$i2,"id");

$img=mysql_result($result2,$i2,"img");

 

$price=mysql_result($result2,$i2,"price");

 

$desc2=mysql_result($result2,$i2,"description");

$desc = urldecode($desc2);

 

$item_name=mysql_result($result2,$i2,"name");

$name2 = urldecode($item_name);

 

 

 

 

 

?>

<div style="padding: 5px; background-color: #fff;">

<img src="<?php echo $img;?>" width="18%"  style="padding: 5px; margin-right: 5px; border: 1px solid #663398; background-color: #FFF;" align="left" /><h2 style="padding:0; margin:0;"><?php echo $name2;?></h2>

<a href="cart.php?action=delete&id=<?php echo $id;?>" class="r">Remove</a>

<input type="text" name="qty<?php echo $id;?>" value="<?php echo $qty;?>" size="3" maxlength="3" />

<? echo '$'.($price * $qty);

$total += $price * $qty;

 

?>

<p><?php echo $desc;?><p></div>

 

 

 

 

<?

 

}

 

 

 

echo  '<div style=""><p><h2>Grand total: <strong>$'.$total. '</strong></h2></p>';

echo  '<p><input type="submit" value="Update cart"/></form>';

echo '<form action="phpauthnet_aim/orderform.php"><input type="hidden" name="cost" value='.$total.'>If everything is good: <input type="submit" value="proceed to checkout"/></form></p></div>';

 

 

 

} else {

$output[] = '<p>You shopping cart is empty.</p>';

}

return join('',$output);

}

?>

 

 

Any help would be wonderfully received and a whole bowl of strawberry karma will be waiting for you...

 

 

Link to comment
Share on other sites

The main info is generated in the showCart() function.

The problem is the result from the query only gives me the name.  The qty I am getting from an array created from the $contents = array() and am having troubles with putting the two together...  again my brain is rapidly deteriorating.

 

Does this help?

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.