Solved

How to break an array into individual variables

Posted on 2006-10-22
2
200 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:gamebits
2 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 17783825
This should give you $var1, $var2, etc...
<?

                $section_id=array(); $section_id=$_POST['Record_id'];            

                     foreach($section_id as $value) {
                     
$query1 = mysql_query("SELECT street
                                    FROM City
                                    WHERE section_id = '$value'");
 $i = 1;                      
 while($row2 = mysql_fetch_array($query1))
 
 echo "<p>".$row2['street']."";
 $var = 'var'.$i;
 $$var = $row2['street'];
 $i++;
  }
 
?>
0
 
LVL 28

Author Comment

by:gamebits
ID: 17784791
TeRReF

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_id'];            

                     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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now