Solved

adding items from database into an array

Posted on 2007-11-20
5
232 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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
The viewer will learn how to count occurrences of each item in an array.

770 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