473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Populating Arrays via code

25
Hi,
How do I populate a flat array through a loop type sequence.
So far I have a basic query resulting in a few rows of data.

$sSql = 'SELECT suburb'
. ' FROM locations'
. ' WHERE locations.postcode = '.$postcode
. ' LIMIT 0 , 40;';

$result = mysql_query($sSql) or die('Query failed: ' . mysql_error());
$numberOfRows = mysql_num_rows($result);
$i = 0;

$SuburbArray = array(); // define the array
if ($numberOfRows>0) {
while ($i<$numberOfRows) {
echo mysql_result($result,$i,"suburb");
$thisSuburb = mysql_result($result,$i,"suburb");
// I need to add each value to array here.
$i++;
}
}


thank you.
Sep 7 '06 #1
2 5753
Hi

Try this,

$result = mysql_query($sSql) or die('Query failed: ' . mysql_error());
$numberOfRows = mysql_num_rows($result);
$i = 0;

$SuburbArray = array(); // define the array
if ($numberOfRows>0) {
while ($i<$numberOfRows) {
echo mysql_result($result,$i,"suburb");
$thisSuburb = mysql_result($result,$i,"suburb");
// I need to add each value to array here.

/*********************************************/
$SuburbArray[i] = $thisSuburb;

/**********************************************/



$i++;
}
}



You can check this , by printing the array values
...
$total_count = count($SuburbArray);
for($index=0;$index<$total_count;$index++)
{
echo $SuburbArray[$index]."<br>";
}
...

Try this and reply.

-somaskarthic


Hi,
How do I populate a flat array through a loop type sequence.
So far I have a basic query resulting in a few rows of data.

$sSql = 'SELECT suburb'
. ' FROM locations'
. ' WHERE locations.postcode = '.$postcode
. ' LIMIT 0 , 40;';

$result = mysql_query($sSql) or die('Query failed: ' . mysql_error());
$numberOfRows = mysql_num_rows($result);
$i = 0;

$SuburbArray = array(); // define the array
if ($numberOfRows>0) {
while ($i<$numberOfRows) {
echo mysql_result($result,$i,"suburb");
$thisSuburb = mysql_result($result,$i,"suburb");
// I need to add each value to array here.
$i++;
}
}


thank you.
Sep 8 '06 #2
adriann
25
Hey, thanks for that. That works!
I also found another solution.

At the section of code where each new item is to be added into the array...
array_push($SuburbsArray, $NewItem)



regards,
Adrian
Sep 8 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
2
by: Prime | last post by:
Hi All ... I'm writing a small shopping cart app (school assignment) and need to list all the books that a user selects. I have created a "BookBasket" object that contains among other things...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
1
by: Mike P | last post by:
I am populating a drop down column in a datagrid on page load. Here is my code : <asp:TemplateColumn> <ItemTemplate> <asp:DropDownList ID="ddlUserName" Font-Name="Verdana" Font-Size="8pt"...
8
by: jack-b | last post by:
Hi, I have a list box which displays countries names and a second listbox which displays their cites (based on the selection made in ListBox 1) If the user selects USA i want to display cities...
1
by: Stephene | last post by:
New to the world of web design/php/mysql and need help please. What I'm trying to do: I would like a web page with three drop down menus each populated by a query The first represents...
4
by: clrockwell | last post by:
Hey all, I could use some help with solving problem. Any suggestions are welcome and very appreciated. I have two arrays, $feeds and $feedDays. If they both have the same count, or $feeds...
4
by: Peter Duniho | last post by:
On Thu, 14 Aug 2008 18:56:00 -0700, Phill <Phill@discussions.microsoft.comwrote: For future reference, if you are asking for help with an error (compile or execution), you really should post...
6
by: cathyisfrustrated | last post by:
Hey, I've been trying to learn javascript for a few days. I'm a bit stuck! I'd appreciate any links to get me going in the right direction or any advice I can get. I've written a...
7
by: phpnewbie26 | last post by:
Hi! I'm new to PHP and I have a question about generating parent-child drop down menus. I have seen many forums on how to generate them from databases, but not many from arrays. Let's say the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.