Simplifying code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Simplifying code

Post 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 :)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Use the same code you usually use to get rows from the database (mysq_fetch_*), and then something like:
$attack[] = $row[0];
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$attack = array();
while($row = mysql_fetch_assoc($res)) {
  $attack[] = $row['won'];
}
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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]; 
// .....
?>
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

How can i do that without writing it out 10 seperate times?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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;
}
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Read my first post ;)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Your question has changed since your first post -- what are you asking?
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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]; 
// .....
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>';
}
?>
Post Reply