Page 1 of 1

mysql and arrays

Posted: Thu Dec 28, 2006 6:05 pm
by SidewinderX
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've created a connection to a my database to display the top 5 users based upon a particular statistic I specified.

Code: Select all

<?php
$db2 = mysql_connect("", "", "");
mysql_select_db("stssquad_squadstats", $db2) or die('Error : ' . mysql_error()); ;
$result2 = mysql_query("SELECT * FROM bhd_stats ORDER BY total_kills DESC", $db2);
for ($n = 0; $n < 5; $n++) {
	$row = mysql_fetch_assoc($result2);
	echo $row['player_name'] . "<br>";
}
?>
That will display something like this:
sTs-HaWk-.
sTs-Helmet
sTs-Arsenal06
sTs-Neji
sTs-Stink
What I want to do is store those values as an array so i can reference sTs-HaWk-. as $number[0]. That way I will be able to use it in my header with ease

Code: Select all

<table width="1024" border="0" cellpadding="0" cellspacing="0" align="center">
	<tr>
		<td rowspan="8" background="themes/Players/images/header_01.jpg" width="8" height="309"></td>
		<td rowspan="8">
			<img src="themes/Players/images/header_02.jpg" width="44" height="309" alt=""></td>
		<td colspan="4">
			<img src="themes/Players/images/header_03.jpg" width="918" height="169" alt=""></td>
		<td rowspan="8">
			<img src="themes/Players/images/header_04.jpg" width="45" height="309" alt=""></td>
		<td rowspan="8" background="themes/Players/images/header_05.jpg" width="9" height="309"></td>
	</tr>
	<tr>
		<td>
			<img src="themes/Players/images/header_06.jpg" width="285" height="17" alt=""></td>
		<td>
			<img src="themes/Players/images/header_07.jpg" width="207" height="17" alt=""></td>
		<td>
			<img src="themes/Players/images/header_08.jpg" width="209" height="17" alt=""></td>
		<td>
			<img src="themes/Players/images/header_09.jpg" width="217" height="17" alt=""></td>
	</tr>
	<tr>
		<td rowspan="5" background="themes/Players/images/header_10.jpg" width="285" height="83">
			<?php echo $theuser; ?>
		</td>
		<td background="themes/Players/images/header_11.jpg" width="207" height="18" class="s1"><div align="center"><?php $number[0]; ?></div></td>
		<td background="themes/Players/images/header_12.jpg" width="209" height="18" class="s2"><div align="center">1. Sidewinder</div></td>
		<td background="themes/Players/images/header_13.jpg" width="217" height="18" class="s3"><div align="center">1. Sidewinder</div></td>
	</tr>
	<tr>
		<td background="themes/Players/images/header_14.jpg" width="207" height="17" class="s1"><div align="center"><?php $number[1]; ?></div></td>
		<td background="themes/Players/images/header_15.jpg" width="209" height="17" class="s2"><div align="center">2. Sidewinder</div></td>
		<td background="themes/Players/images/header_16.jpg" width="217" height="17" class="s3"><div align="center">2. Sidewinder</div></td>
	</tr>
	<tr>
		<td background="themes/Players/images/header_17.jpg" width="207" height="15" class="s1"><div align="center"><?php $number[2]; ?></div></td>
		<td background="themes/Players/images/header_18.jpg" width="209" height="15" class="s2"><div align="center">3. Sidewinder</div></td>
		<td background="themes/Players/images/header_19.jpg" width="217" height="15" class="s3"><div align="center">3. Sidewinder</div></td>
	</tr>
	<tr>
		<td background="themes/Players/images/header_20.jpg" width="207" height="17" class="s1"><div align="center"><?php $number[3]; ?></div></td>
		<td background="themes/Players/images/header_21.jpg" width="209" height="17" class="s2"><div align="center">4. Sidewinder</div></td>
		<td background="themes/Players/images/header_22.jpg" width="217" height="17" class="s3"><div align="center">4. Sidewinder</div></td>
	</tr>
	<tr>
		<td background="themes/Players/images/header_23.jpg" width="207" height="16" class="s1"><div align="center"><?php $number[4]; ?></div></td>
		<td background="themes/Players/images/header_24.jpg" width="209" height="16" class="s2"><div align="center">5. Sidewinder</div></td>
		<td background="themes/Players/images/header_25.jpg" width="217" height="16" class="s3"><div align="center">5. Sidewinder</div></td>
	</tr>
	<tr>
		<td colspan="4">
			<img src="themes/Players/images/header_26.jpg" width="918" height="40" alt=""></td>
	</tr>
</table>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: mysql and arrays

Posted: Thu Dec 28, 2006 6:22 pm
by Kieran Huggins

Code: Select all

$db2 = mysql_connect("", "", "");
mysql_select_db("stssquad_squadstats", $db2) or die('Error : ' . mysql_error()); ;
$result2 = mysql_query("SELECT * FROM bhd_stats ORDER BY total_kills DESC LIMIT 5", $db2);
while ($row = mysql_fetch_assoc($result2)) {
        $player[] = $row;
}
?>
now you should be able to:

Code: Select all

echo 'the best: '.$player[0]['player_name'];
echo 'also good: '.$player[1]['player_name'];
echo '5th place : '.$player[4]['player_name'];

echo 'did you know that '.$player[2]['player_name'].' killed '.$player[2]['total_kills'].' people? How disturbing!';

Posted: Thu Dec 28, 2006 6:32 pm
by SidewinderX
Perfect! Thats exactley what I was trying to do. Thank you :D