Solved

adding items from database into an array

Posted on 2007-11-20
5
249 Views
Last Modified: 2008-02-01
hi, i am using this code to select from a database,


                              $query = "Select * from countys;";
                              $result1 = mysql_query($query);
                        
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    }
      
                              }


i want to put each item into an array, like this,

$option = array('--','County1','County2') etc etc...

how do i build the array option in the database code above?


0
Comment
Question by:markmchugh
[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
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20320756
$query = "Select county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
$option[]=$myrow1[0];
                                    }
     
                              }
0
 
LVL 82

Expert Comment

by:hielo
ID: 20320799
IF your query is returning more than one field, as in:
$query = "Select city, county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_assoc($result1))
                                    {
$temp=array("city" => $myrow1["city"], "county" =>$myrow1["county"] );
$option[]=$temp;
                                    }
     
You would then retrieve the values as follows:
for($i=0;$i<count($option);++$i)
{
echo( $option[$i]["city"] . " " . $options[$i]["county"]);
}
                              }

0
 
LVL 4

Expert Comment

by:simonkin
ID: 20321506
Hi,

Try this...


<?php
 
	// Perform the query
	$query = "Select * from countys;";
	$result = mysql_query($query);
                        
	// If there are results
	if (mysql_num_rows($result) > 0) {
	
		// Define the options array
		$options = array();
	
		// Loop through the resultset
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		
			// Write the options array
			$options[] = $row["county"]; // Assuming county is the name of the field you require...
		
		}
		
		// Check the output...
		print_r($options);
	
	}
 
?>

Open in new window

0
 

Author Comment

by:markmchugh
ID: 20321726
i want to put '--' a first position in the array, i'm using this for a select box,

this code does not do it

$option[] = '--';
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    $option[]=$myrow1[1];
                                    }
      
0
 
LVL 4

Accepted Solution

by:
simonkin earned 500 total points
ID: 20321775
Ok, try this...


<?php
 
        // Perform the query
        $query = "Select * from countys;";
        $result = mysql_query($query);
                        
        // If there are results
        if (mysql_num_rows($result) > 0) {
 
                // Define the options array
                $options = array();
 
                // Add the -- to the start of the array
                $options[] = "--";
        
                // Loop through the resultset
                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                
                        // Write the options array
                        $options[] = $row["county"]; // Assuming county is the name of the field you require...
                
                }
                
                // Check the output...
                print_r($options);
        
        }
 
?>

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 40
How to get this library to work load? 8 40
Google Dork query 7 66
Echo values after a query in php 5 26
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

726 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