Page 1 of 2

Simplifying code

Posted: Mon Sep 11, 2006 11:26 am
by Dave2000

Code: Select all

$attack1 = mysql_result($result,0,"won");
$attack2 = mysql_result($result,1,"won");
$attack3 = mysql_result($result,2,"won");
$attack4 = mysql_result($result,3,"won");
$attack5 = mysql_result($result,4,"won");
$attack6 = mysql_result($result,5,"won");
$attack7 = mysql_result($result,6,"won");
$attack8 = mysql_result($result,7,"won");
$attack9 = mysql_result($result,8,"won");
$attack10 = mysql_result($result,9,"won");
I am attempting to simplify the above code. I came up with the below, but it keep producing an internal server error. Is this due to a problem with the code, and if so, how can it be fixed.

Code: Select all

for ($i=1; $i <= 10; $i++) {

$a = $i - 1;

$attack.$i = mysql_result($result,$a,"won");

}
Thank you

Shears :)

Posted: Mon Sep 11, 2006 11:33 am
by Mordred
Why don't you use an array ($attack[0] etc?)

Otherwise, the correct syntax is this:

Code: Select all

${"attack$i"} = mysql_result($result, $i-1, 'won');
but keep in mind that it is both perverse and unneeded.

Posted: Mon Sep 11, 2006 12:46 pm
by Dave2000

Code: Select all

$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");
Does that help? ^^ The query. I am wishing to asign $attack1, $attack2 etc to different rows. It is NOT different fields from the same row., It IS different rows from the same field. Maybe i could still do i differently but i dont really see how. I would know if it was different fields from the same row but it's not that.

Posted: Mon Sep 11, 2006 1:28 pm
by Mordred
Use the same code you usually use to get rows from the database (mysq_fetch_*), and then something like:
$attack[] = $row[0];

Posted: Mon Sep 11, 2006 1:51 pm
by Dave2000
Yes that is part of the question - what code to use. Up to now i've just been using mysql_result($result, $i-1, 'won'); but it's always before been part of some loop, maybe even that is still inefficient. :( So what to you recommend?

Thank you

Shears :)

Posted: Mon Sep 11, 2006 1:59 pm
by Weirdan

Code: Select all

$attack = array();
while($row = mysql_fetch_assoc($res)) {
  $attack[] = $row['won'];
}

Posted: Sat Sep 16, 2006 9:14 pm
by Dave2000
Are you saying that ^^ is the same as this...

Code: Select all

for ($i=1; $i <= 10; $i++) {
${"attack$i"} = mysql_result($result, $i-1, 'won');
}
Currently - my method works, and yours doesn't. However, if your method if more efficient then i would be interested to know how to do it - but at the moment, it just wont work

Shears :?

Posted: Sat Sep 16, 2006 9:20 pm
by aaronhall
Your references to the variables need to change from $attack1, $attack2, ..., $attack10 to $attack[0], $attack[1], ..., $attack[9]

Bring it all together

Code: Select all

<?php
$attack = array();
$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");

while($row = mysql_fetch_assoc($result)) { // this will loop through every row in $result
	$attack[] = $row['won'];
}

echo $attack[0];
echo $attack[1];
echo $attack[2]; 
// .....
?>

Posted: Sat Sep 16, 2006 9:33 pm
by Dave2000
How can i do that without writing it out 10 seperate times?

Posted: Sat Sep 16, 2006 9:38 pm
by aaronhall
Don't know what you want to do, but I'll take a wild guess anyway:

Code: Select all

foreach($attack as $key => $val) {
  echo $val;
}

Posted: Sat Sep 16, 2006 9:49 pm
by Dave2000
Read my first post ;)

Posted: Sat Sep 16, 2006 10:04 pm
by aaronhall
Your question has changed since your first post -- what are you asking?

Posted: Sat Sep 16, 2006 10:07 pm
by Dave2000
My question is the same as in my first post. What is the most efficient way i can rewrite this...

Code: Select all

$attack1 = mysql_result($result,0,"won");
$attack2 = mysql_result($result,1,"won");
$attack3 = mysql_result($result,2,"won");
$attack4 = mysql_result($result,3,"won");
$attack5 = mysql_result($result,4,"won");
$attack6 = mysql_result($result,5,"won");
$attack7 = mysql_result($result,6,"won");
$attack8 = mysql_result($result,7,"won");
$attack9 = mysql_result($result,8,"won");
$attack10 = mysql_result($result,9,"won");
Thank you

Shears :)

Posted: Sat Sep 16, 2006 10:14 pm
by aaronhall
aaronhall wrote:Your references to the variables need to change from $attack1, $attack2, ..., $attack10 to $attack[0], $attack[1], ..., $attack[9]

Bring it all together

Code: Select all

<?php
$attack = array();
$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");

while($row = mysql_fetch_assoc($result)) { // this will loop through every row in $result
	$attack[] = $row['won'];
}

echo $attack[0];
echo $attack[1];
echo $attack[2]; 
// .....
?>

Posted: Sat Sep 16, 2006 11:06 pm
by RobertGonzalez
Shears wrote:My question is the same as in my first post. What is the most efficient way i can rewrite this...

Code: Select all

$attack1 = mysql_result($result,0,"won");
$attack2 = mysql_result($result,1,"won");
$attack3 = mysql_result($result,2,"won");
$attack4 = mysql_result($result,3,"won");
$attack5 = mysql_result($result,4,"won");
$attack6 = mysql_result($result,5,"won");
$attack7 = mysql_result($result,6,"won");
$attack8 = mysql_result($result,7,"won");
$attack9 = mysql_result($result,8,"won");
$attack10 = mysql_result($result,9,"won");
Thank you

Shears :)
If you read the manual page on mysql_result() it says that for large data sets, mysql_* array functions are probably better. What you are doing in your code is running through a mysql_query result and, row by row telling the script for each row, pull information from the col 'won' and read that into a numbered var that begins $attack. This is inefficent. If you were to use mysql_fetch_array, you would be able to loop the result array and reference these row within the loop.

Code: Select all

<?php
if (!$result = mysql_query($query))
{
    error_out('The query did not work: ' . mysql_error()); // This should be a custom error handler
}

$attacks = array();
while ($attacks[] = mysql_fetch_array($result))
{
    $attacks_count = $count($attacks);
}

// Now loop the attacks array
for ($i = 0; $i < $attacks_count; $i++)
{
    echo '<p>This is row number ' . $i . ' and the value for won is ' . $attacks[$i]['won'] . '.</p>';
}
?>