Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

Learning again...

Recommended Posts

trying to understand FOREACH

in this code, you get a multi-array
(Array ( [0] => 5500.990 [TEK_PART_NUMBER] => 5500.990 )
so when the foreach is echoed out, it lists each item twice, as it has two indexes???
How do you do it so you only get one return per...

(Told you I was learning new things...)

[code]
echo "<table border='1'>";
mysql_select_db($database);
$result1 = mysql_query("SELECT * FROM LBRYPERM WHERE TEK_PART_NUMBER LIKE '5500.99%'");
    while($row1=mysql_fetch_array($result1)){
    echo "<tr>";
    foreach ($row1 as $v) {
        echo "<td>$v</td>";
        }
    echo "</tr>";
    }
echo "</table>";
[/code]

Share this post


Link to post
Share on other sites
can't you do

[code]echo $row1[0]['TEK_PART_NUMBER'][/code]

instead, if $row1 is a 2D array.

Share this post


Link to post
Share on other sites
Change [code]<?php while($row1=mysql_fetch_array($result1)){ ?>[/code] to [code]<?php while($row1=mysql_fetch_assoc($result1)){ ?>[/code]
The [a href=\"http://www.php.net/mysql_fetch_assoc\" target=\"_blank\"]mysql_fetch_assoc()[/a] function returns an associative array while [a href=\"http://www.php.net/mysql_fetch_array\" target=\"_blank\"]mysql_fetch_array()[/a] returns an array that contains both associative and number indices.

Ken

Share this post


Link to post
Share on other sites
[b]mysql_fetch_array()[/b] defaults to returning BOTH numeric and associative arrays, basically meaning that each value will have two keys (one numeric, one string). You could just switch that function over to [b]mysql_fetch_row()[/b], and your code will work as you invisioned it.

If, for some reason, you wanted to continue to use that function while only printing one of each of the values, you'd have to do something like:[code]$row1 = mysql_fetch_array($result1);
foreach ($row1 as $key => $value) {
    if (is_numeric($key)) continue;
    echo "<td>$value</td>";
}[/code]

Also, you could use one of the optional flags to [b]mysql_fetch_array()[/b] (as a second argument): MYSQL_ASSOC or MYSQL_NUM, returning an array with only those types of keys.

Oops, sorry Ken, you beat me to it. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Share this post


Link to post
Share on other sites
I understand so far but I guess what I am stuck on (haven't learned yet) is this part:

$row1 as $key => $value

I only kinda grasp the $row1 as $key par
but the => $value part I don't have a clue about...

what do you call that type of operand? or whatever "=>" means...

I see it a lot, and I know it's used a lot in OOP, but don't know what it means

Any help would be greatly appreciated...

here is what I learned from you so far

while($row1=mysql_fetch_array($result1)){ //keyed by number and by field name
while($row1=mysql_fetch_row($result1)){ //keyed by number
while($row1=mysql_fetch_assoc($result1)){ //keyed by field name

Share this post


Link to post
Share on other sites
Please see the [a href=\"http://www.php.net/manual/en/control-structures.foreach.php\" target=\"_blank\"]explanation[/a] in the fine manual about this very subject.

Ken

Share this post


Link to post
Share on other sites
I have explained what this operator does in the [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95867\" target=\"_blank\"]FAQ/Code Respository[/a]. Its after I explain the -> operator.

Share this post


Link to post
Share on other sites
When a guy doesn't get what he read in the manual, so he asks a question, how does he do it in a way that people don't get irritated at him, and assume he didn't look at the manual? I'm not young any more... things don't come as easy as they used to :-/

Wild, while I know you worked very hard on your tutorial, at it has no doubt helped many folks, it is written at a level higher than my current level or understanding (OOP is hard for me). So, while it is a good tutorial, I have to read several other tutorials to understand what you wrote... Sorry I'm not smarter...

Share this post


Link to post
Share on other sites
hehe... I gues this just proves your point.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--](OOP is hard for me)[/quote]
The => operator has nothing to do with OOP.

Actaully. I just read Wildteens explination of the => operator when used within a foreach and yeah... its not very clear.

The => operator is normally used to assign a value to an array key... eg;
[code]
$arr = array('key' => 'val');
echo $arr['key']; // produces val
[/code]
It works exactly the same in a foreach.
[code]
foreach ($arr as $key => $val)
[/code]
Think of that as saying (in english).

For each element of the array stored in $arr, assign its key to the variable $key and its value to the variable $val.

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]When a guy doesn't get what he read in the manual, so he asks a question, how does he do it in a way that people don't get irritated at him, and assume he didn't look at the manual? I'm not young any more... things don't come as easy as they used to :-/[/quote]
Basically, I would say "I've looked at the manual section that describes XYZ and I don't understand what this does or how to use it".

If you don't tell us that you've read the relavent manual section(s), we can only assume that you didn't.

BTW, as for getting older, I just passed the birthday that put me at the old US National Speed Limit....

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.