Skip to main content
Inspiring
October 25, 2006
Question

OT: PHP looping through an array

  • October 25, 2006
  • 30 replies
  • 1620 views
When I build a recordset, I create an array, right?

So, if I have this -

mysql_select_db($database_touchstone, $touchstone);
$query_rsCategory = "SELECT DISTINCT tclientsCategory FROM
tbl_orderedclients";
$rsCategory = mysql_query($query_rsCategory, $touchstone) or
die(mysql_error());
$row_rsCategory = mysql_fetch_assoc($rsCategory);
$totalRows_rsCategory = mysql_num_rows($rsCategory);

The variable called $row_rsCategory contains an array of all values in the
recordset, right?

So, why can't I get those values like this?

foreach ($row_rsCategory as $value) {
echo $value."<br>";
}


--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================



This topic has been closed for replies.

30 replies

Inspiring
October 25, 2006
Nice. I like that, and have stolen it for my own personal exploitation.
Muhahahhahaha!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:0a7vj2l3h6npmtadu9rjkp5g4slrqnt439@4ax.com...
> .oO(crash)
>
>>Can you think of a quick easy way to put this in an external function?
>>
>>I was wanting to have a file which would take a recordset from a page,
>>then
>>create an array (from a function), then spit back the array so that I
>>could
>>work with one array within the page. Does that sound do-able or just
>>overly
>>complicated?
>
> Something like this?
>
> <?php
> function mysqlFetchAll($rs, $resultType = MYSQL_BOTH) {
> $result = array();
> while ($record = mysql_fetch_array($rs, $resultType)) {
> $result[] = $record;
> }
> return $result;
> }
> ?>
>
> Allowed values for the optional $resultType parameter are MYSQL_ASSOC,
> MYSQL_NUM and MYSQL_BOTH.
>
> http://www.php.net/manual/en/function.mysql-fetch-array.php
>
> JFTR: The PDO extension already has such a function, but PDO is not
> available on all servers.
>
> http://www.php.net/manual/en/function.pdostatement-fetchall.php
>
> Micha


Inspiring
October 25, 2006
.oO(crash)

>Can you think of a quick easy way to put this in an external function?
>
>I was wanting to have a file which would take a recordset from a page, then
>create an array (from a function), then spit back the array so that I could
>work with one array within the page. Does that sound do-able or just overly
>complicated?

Something like this?

<?php
function mysqlFetchAll($rs, $resultType = MYSQL_BOTH) {
$result = array();
while ($record = mysql_fetch_array($rs, $resultType)) {
$result[] = $record;
}
return $result;
}
?>

Allowed values for the optional $resultType parameter are MYSQL_ASSOC,
MYSQL_NUM and MYSQL_BOTH.

http://www.php.net/manual/en/function.mysql-fetch-array.php

JFTR: The PDO extension already has such a function, but PDO is not
available on all servers.

http://www.php.net/manual/en/function.pdostatement-fetchall.php

Micha
Inspiring
October 25, 2006
In the code I gave you, $categories[$i][0] would contain all of the
recordset's values for each value of $i up to the number of records, no?
Why can't you manipulate that?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"crash" <crash@bcdcdigital.com> wrote in message
news:eho4s6$r1d$1@forums.macromedia.com...
>I tried that, can't pass it to a function outside of the page? Unless I
>made a mistake I'm not catching - I made these files almost a month ago, I
>think.
>
> What I have is this:
>
> inside my page
> do/while that feeds my data to outside functions, which then populate the
> html inside the do/while loop.
>
> do
> set data up through outside functions
> while
>
> What I would much rather do is have a single function on my page, so
>
> create_listing($recordset);
>
> but I don't seem to be able to pull a "recordset block" out of the page
> and into my functions.
>
> Does that make sense?
>
> Jon
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:eho3rp$pq5$1@forums.macromedia.com...
>> Multi-dimensional even -
>>
>> do {
>> foreach ($row_rsCategory as $value) {
>> $categories[][0] = $value;
>> }
>> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
>>
>>
>> --
>> Murray --- ICQ 71997575
>> Adobe Community Expert
>> (If you *MUST* email me, don't LAUGH when you do so!)
>> ==================
>> http://www.dreamweavermx-templates.com - Template Triage!
>> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
>> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
>> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
>> ==================
>>
>>
>> "crash" <crash@bcdcdigital.com> wrote in message
>> news:eho3mc$pgi$1@forums.macromedia.com...
>>> David-
>>>
>>> Can you think of a quick easy way to put this in an external function?
>>>
>>> I was wanting to have a file which would take a recordset from a page,
>>> then create an array (from a function), then spit back the array so that
>>> I could work with one array within the page. Does that sound do-able or
>>> just overly complicated?
>>>
>>>
>>>> You need to extract each row from your recordset with a loop in the
>>>> same way as a Dreamweaver repeat region does.
>>>>
>>>> <?php
>>>> do {
>>>> foreach ($row_rsCategory as $value) {
>>>> echo $value."<br>";
>>>> }
>>>> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
>>>> ?>
>>>>
>>>>
>>>> --
>>>> David Powers
>>>> Adobe Community Expert
>>>> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
>>>> http://foundationphp.com/
>>>
>>>
>>
>>
>
>


Inspiring
October 25, 2006
I tried that, can't pass it to a function outside of the page? Unless I
made a mistake I'm not catching - I made these files almost a month ago, I
think.

What I have is this:

inside my page
do/while that feeds my data to outside functions, which then populate the
html inside the do/while loop.

do
set data up through outside functions
while

What I would much rather do is have a single function on my page, so

create_listing($recordset);

but I don't seem to be able to pull a "recordset block" out of the page and
into my functions.

Does that make sense?

Jon
"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:eho3rp$pq5$1@forums.macromedia.com...
> Multi-dimensional even -
>
> do {
> foreach ($row_rsCategory as $value) {
> $categories[][0] = $value;
> }
> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
>
>
> --
> Murray --- ICQ 71997575
> Adobe Community Expert
> (If you *MUST* email me, don't LAUGH when you do so!)
> ==================
> http://www.dreamweavermx-templates.com - Template Triage!
> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
> ==================
>
>
> "crash" <crash@bcdcdigital.com> wrote in message
> news:eho3mc$pgi$1@forums.macromedia.com...
>> David-
>>
>> Can you think of a quick easy way to put this in an external function?
>>
>> I was wanting to have a file which would take a recordset from a page,
>> then create an array (from a function), then spit back the array so that
>> I could work with one array within the page. Does that sound do-able or
>> just overly complicated?
>>
>>
>>> You need to extract each row from your recordset with a loop in the same
>>> way as a Dreamweaver repeat region does.
>>>
>>> <?php
>>> do {
>>> foreach ($row_rsCategory as $value) {
>>> echo $value."<br>";
>>> }
>>> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
>>> ?>
>>>
>>>
>>> --
>>> David Powers
>>> Adobe Community Expert
>>> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
>>> http://foundationphp.com/
>>
>>
>
>


Inspiring
October 25, 2006
Multi-dimensional even -

do {
foreach ($row_rsCategory as $value) {
$categories[][0] = $value;
}
} while($row_rsCategory = mysql_fetch_assoc($rsCategory));


--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"crash" <crash@bcdcdigital.com> wrote in message
news:eho3mc$pgi$1@forums.macromedia.com...
> David-
>
> Can you think of a quick easy way to put this in an external function?
>
> I was wanting to have a file which would take a recordset from a page,
> then create an array (from a function), then spit back the array so that I
> could work with one array within the page. Does that sound do-able or
> just overly complicated?
>
>
>> You need to extract each row from your recordset with a loop in the same
>> way as a Dreamweaver repeat region does.
>>
>> <?php
>> do {
>> foreach ($row_rsCategory as $value) {
>> echo $value."<br>";
>> }
>> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
>> ?>
>>
>>
>> --
>> David Powers
>> Adobe Community Expert
>> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
>> http://foundationphp.com/
>
>


Inspiring
October 25, 2006
David-

Can you think of a quick easy way to put this in an external function?

I was wanting to have a file which would take a recordset from a page, then
create an array (from a function), then spit back the array so that I could
work with one array within the page. Does that sound do-able or just overly
complicated?


> You need to extract each row from your recordset with a loop in the same
> way as a Dreamweaver repeat region does.
>
> <?php
> do {
> foreach ($row_rsCategory as $value) {
> echo $value."<br>";
> }
> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
> ?>
>
>
> --
> David Powers
> Adobe Community Expert
> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
> http://foundationphp.com/


Inspiring
October 25, 2006
Murray *ACE* wrote:
> Pass the dunce cap, will you? 8)

Here you are: <:(

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Inspiring
October 25, 2006
Pass the dunce cap, will you? 8)

Thanks much, David.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"David Powers" <david@example.com> wrote in message
news:eho0lk$lps$1@forums.macromedia.com...
> Murray *ACE* wrote:
>> When I build a recordset, I create an array, right?
>
> Wrong. You create a result resource.
>
>> The variable called $row_rsCategory contains an array of all values in
>> the recordset, right?
>
> Wrong again. $row_rsCategory is an associative array containing a single
> row.
>
>> So, why can't I get those values like this?
>>
>> foreach ($row_rsCategory as $value) {
>> echo $value."<br>";
>> }
>
> For the reasons stated above, all that will do is display the contents of
> the first row.
>
> You need to extract each row from your recordset with a loop in the same
> way as a Dreamweaver repeat region does.
>
> <?php
> do {
> foreach ($row_rsCategory as $value) {
> echo $value."<br>";
> }
> } while($row_rsCategory = mysql_fetch_assoc($rsCategory));
> ?>
>
>
> --
> David Powers
> Adobe Community Expert
> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
> http://foundationphp.com/


Inspiring
October 25, 2006
Murray *ACE* wrote:
> When I build a recordset, I create an array, right?

Wrong. You create a result resource.

> The variable called $row_rsCategory contains an array of all values in the
> recordset, right?

Wrong again. $row_rsCategory is an associative array containing a single
row.

> So, why can't I get those values like this?
>
> foreach ($row_rsCategory as $value) {
> echo $value."<br>";
> }

For the reasons stated above, all that will do is display the contents
of the first row.

You need to extract each row from your recordset with a loop in the same
way as a Dreamweaver repeat region does.

<?php
do {
foreach ($row_rsCategory as $value) {
echo $value."<br>";
}
} while($row_rsCategory = mysql_fetch_assoc($rsCategory));
?>


--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Inspiring
October 25, 2006
heheheheeh, I asked this a month ago. I am anxiously awaiting the answer...


"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ehnv6s$k1c$1@forums.macromedia.com...
> When I build a recordset, I create an array, right?
>
> So, if I have this -
>
> mysql_select_db($database_touchstone, $touchstone);
> $query_rsCategory = "SELECT DISTINCT tclientsCategory FROM
> tbl_orderedclients";
> $rsCategory = mysql_query($query_rsCategory, $touchstone) or
> die(mysql_error());
> $row_rsCategory = mysql_fetch_assoc($rsCategory);
> $totalRows_rsCategory = mysql_num_rows($rsCategory);
>
> The variable called $row_rsCategory contains an array of all values in the
> recordset, right?
>
> So, why can't I get those values like this?
>
> foreach ($row_rsCategory as $value) {
> echo $value."<br>";
> }
>
>
> --
> Murray --- ICQ 71997575
> Adobe Community Expert
> (If you *MUST* email me, don't LAUGH when you do so!)
> ==================
> http://www.dreamweavermx-templates.com - Template Triage!
> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
> ==================
>
>
>