Click here to get back home

Help with arrays

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with arrays inexion 06-16-2008
Get Chitika Premium
Posted by inexion on June 16, 2008, 11:43 am
Please log in for more thread options
Hi,

I will just give an example of what my code looks like and what I'd
like to accomplish.


I have a few arrays that get filled up with mysql table values, and
one thats predefined. my goal is to generate text files with this so
heres how it works...

$array1 = sql data;
$array2 = sql data;

$arr['html code'] = "<html>\n
<body>\n
<div>(one array1 value at a time is filled into this div)</div>\n
<br />\n
<div>(one array2 value at a time is filled into this div)</div>\n
</html>";

for($i = 0; $i < sizeof($array1); $i++)
{
$fwrite(....);
}


so then, my final text file looks like this:

array1_value1
array1_value2
array1_value3
.......

array2_value1
array2_value2
array2_value3
........

I need help with getting the array values from 1 & 2 into the
$array['html code'] so that when the loop happens it can write however
many sql entries there are. thanks!



Posted by Jerry Stuckle on June 16, 2008, 1:51 pm
Please log in for more thread options
inexion wrote:
> Hi,
>
> I will just give an example of what my code looks like and what I'd
> like to accomplish.
>
>
> I have a few arrays that get filled up with mysql table values, and
> one thats predefined. my goal is to generate text files with this so
> heres how it works...
>
> $array1 = sql data;
> $array2 = sql data;
>
> $arr['html code'] = "<html>\n
> <body>\n
> <div>(one array1 value at a time is filled into this div)</div>\n
> <br />\n
> <div>(one array2 value at a time is filled into this div)</div>\n
> </html>";
>
> for($i = 0; $i < sizeof($array1); $i++)
> {
> $fwrite(....);
> }
>
>
> so then, my final text file looks like this:
>
> array1_value1
> array1_value2
> array1_value3
> .......
>
> array2_value1
> array2_value2
> array2_value3
> ........
>
> I need help with getting the array values from 1 & 2 into the
> $array['html code'] so that when the loop happens it can write however
> many sql entries there are. thanks!
>
>
>

I guess my first question would be - why use $array1 and $array2? I
would think something like:

$array[0] = sql data
$array[1] = sql data

Now you can reference them via

$array[0][0]
$array[0][1]
$array[0][2]
$array[1][0]
$array[1][1]
$array[1][2]

and so on. And a simple loop such as

The next question - your sample output shows you want it in the above
format, in which case the following would work:

for ($i = 0; $i < count($array); $i++)
for ($j = 0; $j < count($array[$i]); $j++)
$htmlcode .= "<div></div>\n";

But your sample code seems to indicate you want it in the following format:

$array[0][0]
$array[1][0]
$array[0][1]
$array[1][1]
$array[0][2]
$array[1][2]

In which case you'd want something like:

for ($i = 0; $i < count($array[0]); $i++) {
$htmlcode .= "<div></div>\n";
$htmlcode .= "<div></div>\n";
}

(In the latter case, if the arrays may be different sizes you'll have to
handle that, also).

Also, I'm wondering - why are you putting the code in an array
($arr['html code'])? All you need is a string (which is what I did above).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by inexion on June 16, 2008, 2:46 pm
Please log in for more thread options
If I use that method, it only prints out the first element of each, I
need all 2000+ to be output.....not sure why, but the count goes from
2303 to 1 if I change the array to $array[0]...etc

thanks

Posted by Jerry Stuckle on June 16, 2008, 5:20 pm
Please log in for more thread options
inexion wrote:
> If I use that method, it only prints out the first element of each, I
> need all 2000+ to be output.....not sure why, but the count goes from
> 2303 to 1 if I change the array to $array[0]...etc
>
> thanks
>

No, it will print out all elements of the array. Try the code I gave you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Similar ThreadsPosted
Problem With Arrays Of Arrays, PHP 5.2.0, Windows January 29, 2007, 8:01 am
Indexing non-arrays as arrays, no warnings? April 23, 2007, 8:49 pm
arrays July 17, 2004, 5:33 am
Need help with arrays October 12, 2004, 3:40 am
Use of + with arrays July 6, 2005, 3:45 pm
PHP and Arrays? September 15, 2005, 5:41 pm
php arrays March 20, 2006, 5:05 am
arrays October 3, 2006, 3:52 am
Using arrays September 13, 2007, 5:42 am
How to map arrays? October 16, 2007, 9:41 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap