gamebits
asked on
How to break an array into individual variables
Here is the deal
I have a form where the user can select multiple items using checkboxes after the form is submited the script query the database (MySQL) and pull the data according to the checkboxes selected.
Here is the script to pull the data.
<?
$section_id=array(); $section_id=$_POST['Record _id'];
foreach($section_id as $value) {
$query1 = mysql_query("SELECT street
FROM City
WHERE section_id = '$value'");
while($row2 = mysql_fetch_array($query1) )
echo "<p>".$row2['street']."";
}
?>
The script works great and will echo all the streets on a different line as expected.
But what I need is to be able to break the array in individual parts and assign those parts to variables, so instead of getting something like
Majorstreet
Minorstreet
Sidestreet
I would get
$var1 = "Majorstreet";
$var2 = "Minorstreet";
$var3 = "Sidestreet";
Then I would be able to use the variables where I want in the display page.
Hope it make sense, should not be to hard for an Expert, an easy 500 pts. mostly because I'd like to get an answer asap.
Gamebits
I have a form where the user can select multiple items using checkboxes after the form is submited the script query the database (MySQL) and pull the data according to the checkboxes selected.
Here is the script to pull the data.
<?
$section_id=array(); $section_id=$_POST['Record
foreach($section_id as $value) {
$query1 = mysql_query("SELECT street
FROM City
WHERE section_id = '$value'");
while($row2 = mysql_fetch_array($query1)
echo "<p>".$row2['street']."";
}
?>
The script works great and will echo all the streets on a different line as expected.
But what I need is to be able to break the array in individual parts and assign those parts to variables, so instead of getting something like
Majorstreet
Minorstreet
Sidestreet
I would get
$var1 = "Majorstreet";
$var2 = "Minorstreet";
$var3 = "Sidestreet";
Then I would be able to use the variables where I want in the display page.
Hope it make sense, should not be to hard for an Expert, an easy 500 pts. mostly because I'd like to get an answer asap.
Gamebits
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your solution did not work as is but you put me on the right track, thanks.
Here is the solution that worked for me.
<?
$section_id=array(); $section_id=$_POST['Record
foreach($section_id as $value) {
$query1 = mysql_query("SELECT street
FROM City
WHERE section_id = '$value'");
while($row2 = mysql_fetch_array($query1)
$var[] = "".$row['street']."";
$street1 = "$var[0]";
$street2 = "$var[1]";
?>
Gamebits