Loop through a table. PHP.
- rockinliam
- Posts: 466
- Joined: Sun Jun 07, 2009 11:26 am
Loop through a table. PHP.
How would i loop through a table and the to print out all the fields into a table and then stop when the end of the table is reached?
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
Website: https://liam-griffiths.co.uk/
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Loop through a table. PHP.
When you say "loop" through a table, you mean a MySQL table, correct? If so, here is a simple script that may help show you how to iterate through a multiple-result MySQL query. This was just a test where I made a dummy db table, and made several entries with a "mobid" of 3. I was able to iterate through the results and print them out in a table.
Code: Select all
<html>
<head>
<style type="text/css">
table {
padding=5px;
}
</style>
</head>
<body>
<?php
include 'connect.php';
session_start();
echo "Start of script";
$query = "SELECT * FROM multitest WHERE mobid='3'";
?>
<table border="3" cellpadding="3" cellspacing="0">
<?php
if ($result = mysqli_query($db, $query)) {
//fetch assoc array, and iterate through all of the results
//$row is going to be an INDEXED array, not an ASSOCIATIVE array, so all
//offsets are number indexes, not the names of the columns in the db.
while ($row = mysqli_fetch_row($result)) {
?>
<tr>
<td>ID:</td>
<td><?php echo($row[0]); ?> </td>
</tr>
<tr>
<td>Mob ID:</td>
<td><?php echo($row[1]); ?> </td>
</tr>
<tr>
<td>Name:</td>
<td><?php echo($row[2]); ?> </td>
</tr>
<tr>
<td>Address:</td>
<td><?php echo($row[3]); ?> </td>
</tr>
<tr>
<td>Class:</td>
<td><?php echo($row[4]); ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
mysqli_close($db);
?>
</table>
</body>
</html>The indelible lord of tl;dr
Re: Loop through a table. PHP.
You mean go through the database? I'm not sure it's what you want, but in a highscore code I found once, it used something like
Code: Select all
$playerinfo = "SELECT name, score FROM players";
$playerinfo2 = mysql_query($playerinfo);
while($playerinfo3 = mysql_fetch_array($playerinfo2))
{
// do yo thang har!
}Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
- rockinliam
- Posts: 466
- Joined: Sun Jun 07, 2009 11:26 am
Re: Loop through a table. PHP.
@Jack - Thanks, you got me out of a hole.
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
Website: https://liam-griffiths.co.uk/