Skip to main content
Inspiring
October 25, 2006
Frage

OT: PHP looping through an array

  • October 25, 2006
  • 30 Antworten
  • 1620 Ansichten
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
==================



Dieses Thema wurde für Antworten geschlossen.

30 Antworten

Inspiring
October 25, 2006
but you can use seek to get the next one, yes? is that what you were
thinking of?

"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ehof3c$aeb$1@forums.macromedia.com...
> Neither am I.
>
> I now understand that the fetch array function only gets a single row from
> the table into that array.
>
> --
> 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:23evj29ejpg9d6hdco00pm756octphqsnu@4ax.com...
>> .oO(Murray *ACE*)
>>
>>>Right - but I meant replace the business end of the while statement with
>>>the
>>>fetch_array() function?
>>
>> Hmm? Not sure what you mean.
>>
>> Micha
>
>


Inspiring
October 25, 2006
Neither am I.

I now understand that the fetch array function only gets a single row from
the table into that array.

--
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:23evj29ejpg9d6hdco00pm756octphqsnu@4ax.com...
> .oO(Murray *ACE*)
>
>>Right - but I meant replace the business end of the while statement with
>>the
>>fetch_array() function?
>
> Hmm? Not sure what you mean.
>
> Micha


Inspiring
October 25, 2006
.oO(Murray *ACE*)

>Right - but I meant replace the business end of the while statement with the
>fetch_array() function?

Hmm? Not sure what you mean.

Micha
Inspiring
October 25, 2006
Right - but I meant replace the business end of the while statement with the
fetch_array() function?

--
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:259vj2da8ar2ha36keju1pv7fg82vs7pto@4ax.com...
> .oO(Murray *ACE*)
>
>>Why not use the mysql_fetch_array() function for this?
>
> I was just using my function from the previous post to get all data from
> a recordset into an array first. mysql_fetch_array() just returns a
> single row, not the entire result set.
>
> Micha


Inspiring
October 25, 2006
.oO(Murray *ACE*)

>Why not use the mysql_fetch_array() function for this?

I was just using my function from the previous post to get all data from
a recordset into an array first. mysql_fetch_array() just returns a
single row, not the entire result set.

Micha
Inspiring
October 25, 2006
Micha:

Why not use the mysql_fetch_array() function for this?

--
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:s18vj2h82qhi6041ab591bv21bbe5kpeir@4ax.com...
> .oO(Murray *ACE*)
>
>>Nice. I like that, and have stolen it for my own personal exploitation.
>>Muhahahhahaha!
>
> Thanks for buying online. Please send your money to my PayPal account.
> ;)
>
> Now for part 2:
>
> <?php
> function createListing($rs) {
> $result = '';
> $data = mysqlFetchAll($rs, MYSQL_ASSOC);
> if (!empty($data)) {
> $result .= "<ul>\n";
> foreach ($data as $record) {
> $result .= '<li>';
> $result .= $record['foo']; // output record data here
> $result .= "</li>\n";
> }
> $result .= "</ul>\n";
> }
> return $result;
> }
> ?>
>
> Or something like that.
>
> Micha


Inspiring
October 25, 2006
LMFAO--Micha's looks a little simpiler than mine. ;o)


"Michael Fesser" <netizen@gmx.de> wrote in message
news:s18vj2h82qhi6041ab591bv21bbe5kpeir@4ax.com...
> .oO(Murray *ACE*)
>
>>Nice. I like that, and have stolen it for my own personal exploitation.
>>Muhahahhahaha!
>
> Thanks for buying online. Please send your money to my PayPal account.
> ;)
>
> Now for part 2:
>
> <?php
> function createListing($rs) {
> $result = '';
> $data = mysqlFetchAll($rs, MYSQL_ASSOC);
> if (!empty($data)) {
> $result .= "<ul>\n";
> foreach ($data as $record) {
> $result .= '<li>';
> $result .= $record['foo']; // output record data here
> $result .= "</li>\n";
> }
> $result .= "</ul>\n";
> }
> return $result;
> }
> ?>
>
> Or something like that.
>
> Micha


Inspiring
October 25, 2006
.oO(Murray *ACE*)

>Nice. I like that, and have stolen it for my own personal exploitation.
>Muhahahhahaha!

Thanks for buying online. Please send your money to my PayPal account.
;)

Now for part 2:

<?php
function createListing($rs) {
$result = '';
$data = mysqlFetchAll($rs, MYSQL_ASSOC);
if (!empty($data)) {
$result .= "<ul>\n";
foreach ($data as $record) {
$result .= '<li>';
$result .= $record['foo']; // output record data here
$result .= "</li>\n";
}
$result .= "</ul>\n";
}
return $result;
}
?>

Or something like that.

Micha
Inspiring
October 25, 2006
YES YES YES YES

that's *exactly* what I was looking for, I believe. I did not know the
proper syntax, I guess. I was using mysql_fetch to try to change -ah who
cares what i was doing!! hehehe - thanks mate, I can't wait to get back from
lunch and have another peek at my stuff!

Thanks agian, Micha!

Jon

"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
b/c the do-while was nested inside your original document.

I need a means to pass an object to a function, so I can re-use the
recordset, w/o knowing which recordset it is.

I'll take another look, Murray, after I get back from lunch. I'm starving
and distracted by it. it's possible your code will work - just let me take
another look at it.

Thanks for having a go!

Jon
"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:eho5ot$s7p$1@forums.macromedia.com...
> 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/
>>>>
>>>>
>>>
>>>
>>
>>
>
>