Jump to content

[SOLVED] Arrays make my head hurt


CincoPistolero

Recommended Posts

I want to insert the price and name of an option to a new row in the database. I've figured out how to send one, but not the other. Below is what I have working for just the first option. If anyone can unlock my head's array block, I'd appreciate it.

 

This displays the options and sets the "name" to be sent to a confirmation page that writes to the DB

<?php 
foreach ($_POST['tomBassOpt'] as $value) {
	$tbo=stripslashes($value);
	$query = "SELECT op.optPricesID, op.price, op.optionID, o.name FROM optPrices op, options o WHERE 		op.optPricesID='$tbo' AND op.optionID = o.optionID AND op.drumTypeID=1";
        	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
        	$row = mysql_fetch_row($result);
	echo  $row[3]. "<input type='hidden' name='tomBassOpt[]' value='" .$row[1]. "'>
			<input type='hidden' name='tomBassOptem[]' value='" .$row[3]. "'><br/>";
		}
		  ?>

 

Here is the code on the Confirmation page. It works for the $row[1] value, but I also want to send the $row[3] value to the same row in the db.

<?php
/* ADD Tom/Bass Drum Options Query */
foreach ($_POST['tomBassOpt'] as $tbOpt)
{
$query3 = "INSERT INTO ksOpt (ksOptID, artistID, optPrices, name)
VALUES ('$ksOptID','$aID','$tbOpt',?)";
$result3 = mysql_query($query3) or die ("Error in query: $query3. " . mysql_error());
}
?>

 

The above code here does not write the name to the db.

 

Thanks for any and all help.

Link to comment
Share on other sites

you don't have to let the server assign your index values for you. you can set them explicitly:

 

echo '<input type="hidden" name="tomBassOpt[1]" value="'.$row[1].'" />';

 

i'll be honest and admit that i didn't read through all of your code, but from what you're saying, this sounds like it would solve your issue.

Link to comment
Share on other sites

As stated above, my select statement outputs Name and price, like below.

Custom Brass Head Logo/Artwork :  199

Aquarian Bass Head :  40

Gibraltar Bracket :  45

STANDARD Floor Cradle :  1

Gauger Floor Cradle :  180

 

Right now, my <input type=Hiddens> and the php code that inserts to the db, I can only get to work for the first value. I've been googling all day and can't find out how to insert multiple values. The db I'm inserting too has five fields (ID, aID, opt, optPrice, optType) ID=autoincrement, aID will be sent by a variable and optType will be hard coded. I need opt, and optPrice to be the values sent from the previous page, that is in my foreach loop above. I can get it to display, but HOW do I SEND IT TO NEXT PAGE? Do I need to embed another for loop? do I need to set "x" and "j" counters?  Can I have both field names be the same like below:

<input type='hidden' name='tomBassOpt[]' value='$TBKname'>
<input type='hidden' name='tomBassOpt[]' value='$TBKprice'>

 

I don't want to use serialize(). I want to insert a multidimensional array. I am ok, at inserting single row values, but md arrays send me to a dark closet sucking my thumb.

 

Any help, even up to date website urls is appreciated. I've been googling mysql multidimensional arrays.

Link to comment
Share on other sites

my apologies, the issue is simpler than i thought.

 

when you're going through each tomBassOpt, use the key for each one to access the matching tomBassOptem. assuming i've understood you correctly:

 

foreach ($_POST['tomBassOpt'] AS $key => $this_tomBassOpt)
{
  // this $row[1] value will be in $this_tomBassOpt (just as in $tomBassOpt in your foreach loop)
  // to access the corresponding $row[3] value, simply use:
  $this_tomBassOptem = $_POST['tomBassOptem'][$key];
}

 

since they'll have matching keys (given how you're echoing them into the previous page), all you need to do is grab the key from the tomBassOpt array item, and access the value using that same key in tomBassOptem.

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.