Solved

How to break an array into individual variables

Posted on 2006-10-22
2
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

756 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