visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: can't increment


By: pdd U.S.A.  Date: 25/09/2003 00:00:00  English  Points: 75 Status: Answered
Quality : Excellent
Hi !

ok, i have this code

<?php

while ($nume = mysql_fetch_array($sql_result_numele_p_d)){

$i=0;

echo ("<option value=\"$i\">$nume[$i]</option>\n");

$i++;

}

?>

mysql_fetch_array($sql_result_numele_p_d) returns an array with 18 elements

now my question. why is the value of every <option> tag 0 and how do i increment that whit every new <option> tag
By: Michael701 Date: 25/09/2003 04:16:00 English  Type : Assist
first your $i=0 is INSIDE the while loop, it will reset each time;
do you really want this? or are you trying to use one field from each row as the option?


<?php
$nume = mysql_fetch_array($sql_result_numele_p_d);
$i=0;

foreach ($nume as $value)
{
echo ("<option value=\"$i\">$nume[$i]</option>\n");
$i++;
}
?>

By: VGR Date: 25/09/2003 04:29:00 English  Type : Comment
naaaaa do this :

<?php

$i=0;
while ($nume = mysql_fetch_array($sql_result_numele_p_d)){
echo "<option value=\"$i\">{$nume[$i]}</option>\n";
$i++;
} // while records

?>


PS : NEVER forget to write things like "{$nume[$i]}" when printing array elements in strings :D

By: VGR Date: 25/09/2003 04:31:00 English  Type : Answer
ah sorry, I misread your problem. What a strange idea to access by value of index an associative array when you used mysql_fetch_array()... You could as well have used a non_assoc mysql_* function

<?php

while ($nume = mysql_fetch_array($sql_result_numele_p_d)) for ($i=0;$i<18:$i++) echo "<option value=\"$i\">{$nume[$i]}</option>\n";

?>



By: Michael701 Date: 25/09/2003 04:38:00 English  Type : Comment
VGR, i'm not sure if he wants 18 values from each record or 1 value from 18 records.

moving the $i=0; outside the loop will give him first field from first record, second field from second record.... (very odd)

By: VGR Date: 25/09/2003 17:20:00 English  Type : Comment
he's doing a while (mysql_fetch_array()) and states that "mysql_fetch_array($sql_result_numele_p_d) returns an array with 18 elements"

so I bet for the first solution : X records of 18 elements
By: PHPaul Date: 25/09/2003 17:56:00 English  Type : Comment
VGR, bit off topic, but I've never done this...
echo "<option value=\"$i\">{$nume[$i]}</option>\n";

I'm refering to the {$nume[$i]} piece.
Shouldn't you use echo " bla " . $nume[$i] . "bla"; instead?

thx
By: VGR Date: 25/09/2003 18:05:00 English  Type : Comment
no

both are accepted

I'm just fed up with quoting in and out, so I finally found that PHP offered a simple & elegant way to include also array elements in strings

see php doc

BTW, just try, you'll see what works, and what is shorter to type & easier to use :D
By: VGR Date: 25/09/2003 18:08:00 English  Type : Comment
<A HREF="http://be.php.net/echo">http://be.php.net/echo</a>


echo

(PHP 3, PHP 4 )
echo -- Output one or more strings
Description
[...]
Example 1. echo() examples
[...]
echo "this is {$bar['value']} !"; // this is foo !
By: VGR Date: 25/09/2003 18:09:00 English  Type : Comment
and I thus escape the escaping problem :D
By: pdd Date: 26/09/2003 19:04:00 English  Type : Comment
<?php

$i=0;

while ($nume = mysql_fetch_array($sql_result_numele_p_d)){

$i++;

echo ("<option value=\"$i\">$nume[0]</option>\n");

}

?>

ok, so this what i wanted
but why i have to use $nume[0] ?
By: VGR Date: 26/09/2003 19:44:00 English  Type : Comment
because it's NOT what you wanted !!!

moreover, the echo() line is badly written and I said it multiple times already (array references inside strings)
By: VGR Date: 26/09/2003 19:45:00 English  Type : Comment
and where is your "array of 18 elements" used ? :D :D
By: PHPaul Date: 26/09/2003 20:27:00 English  Type : Comment
lolz, weird way of doing things...

$i++; normally comes at the end of the while block.

I agree with VGR, I don't even do this:
echo "foo = $foo";
I do this...
echo "foo = " . $foo;

This should always be done with arrays, no matter what!!
eg:
echo "foo = " . $foo['some_var'];
or...
echo "foo = {$foo['some_var']}"; as VGR mentioned

--PHPaul
By: VGR Date: 28/09/2003 07:07:00 English  Type : Comment
well, don't be too hard on yourself...

The easiest thing is to write :

echo <<<EOT
here HTML with PHP $variables without any $quoting problem
Only array elements like {$_POST['toto']} need brackets
Apparently, normal array elemets like $thearray[0] will work without {}, althought I wouldn't dare to do it
best $programming ever
EOT;

Do register to be able to answer

EContact
browser fav
page generated in 327.261920 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page