Jump to content


Photo

Learning again...


  • Please log in to reply
9 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 19 June 2006 - 07:08 PM

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...)

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>";


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 June 2006 - 07:20 PM

can't you do

echo $row1[0]['TEK_PART_NUMBER']

instead, if $row1 is a 2D array.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 07:25 PM

Change
<?php while($row1=mysql_fetch_array($result1)){ ?>
to
<?php while($row1=mysql_fetch_assoc($result1)){ ?>
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

#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 19 June 2006 - 07:42 PM

mysql_fetch_array() 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 mysql_fetch_row(), 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:
$row1 = mysql_fetch_array($result1);
foreach ($row1 as $key => $value) {
    if (is_numeric($key)) continue;
    echo "<td>$value</td>";
}

Also, you could use one of the optional flags to mysql_fetch_array() (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\" /]
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 June 2006 - 11:35 AM

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

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 02:57 PM

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

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 June 2006 - 03:07 PM

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.

#8 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 June 2006 - 04:59 PM

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...


#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 June 2006 - 05:24 PM

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;
$arr = array('key' => 'val');
echo $arr['key']; // produces val
It works exactly the same in a foreach.
foreach ($arr as $key => $val)
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.

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 05:37 PM

[!--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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users