Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to break an array into individual variables

Posted on 2006-10-22
2
Medium Priority
?
210 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 1500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

927 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