Page 1 of 1
Mysql table listing (php logic)
Posted: Sun Jul 29, 2007 4:59 am
by spamyboy
Ok, now I list my mysql results like:
Code: Select all
<td id='title'><a href='index.php?word=1'>test</a></td>
<td id='title'><a href='index.php?word=2'>something</a></td>
<td id='title'><a href='index.php?word=3'>ok</a></td>
Code: Select all
while($row = mysql_fetch_array($result))
{
echo "<tr >
<td id='title'><a href='index.php?word=".$row['id']."'>".$row['word']."</a></td>
</tr>";
}
Ok, but now I need to list them smth~ like this:
Code: Select all
<tr>
<td id='title'><a href='index.php?word=1'>test</a></td>
<td id='title'><a href='index.php?word=2'>something</a></td>
<td id='title'><a href='index.php?word=3'>ok</a></td>
</tr>
Good example:
Pleas give example on how to di this.
Posted: Sun Jul 29, 2007 6:13 am
by Ollie Saunders
Code: Select all
if (mysql_num_rows($result)) {
echo '<tr>';
while($row = mysql_fetch_array($result)) {
// removed id="title", an id should be unique
echo '<td><a href="index.php?word=' . $row['id'] . '">' . $row['word'] . '</a></td>';
}
echo '</tr>';
}
Posted: Sun Jul 29, 2007 6:37 am
by spamyboy
You don't get point, it's listed in multyple lines.
like:
Code: Select all
Result1 Result 4 Result 7
Result2 Result 5 Result 8
Result3 Result 6 Result 9
-------------------
Code: Select all
<tr>
<td id='title'><a href='index.php?word=1'>test</a></td>
<td id='title'><a href='index.php?word=2'>something</a></td>
<td id='title'><a href='index.php?word=3'>ok</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=4'>dsfdfs</a></td>
<td id='title'><a href='index.php?word=5'>sosdfsdfmething</a></td>
<td id='title'><a href='index.php?word=6'>sdfsdfsdf</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=7>s</a></td>
<td id='title'><a href='index.php?word=8'>fsdfdsf</a></td>
<td id='title'><a href='index.php?word=9'>dfsdfsdf</a></td>
</tr>
Posted: Sun Jul 29, 2007 8:23 am
by feyd
Both of your examples are different.
Read the first two links in Useful Posts.
Posted: Sun Jul 29, 2007 8:34 am
by Ollie Saunders
spamyboy wrote:You don't get point, it's listed in multyple lines.
like:
Code: Select all
Result1 Result 4 Result 7
Result2 Result 5 Result 8
Result3 Result 6 Result 9
In which case this is incorrect:
spamyboy wrote:Code: Select all
<tr>
<td id='title'><a href='index.php?word=1'>test</a></td>
<td id='title'><a href='index.php?word=2'>something</a></td>
<td id='title'><a href='index.php?word=3'>ok</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=4'>dsfdfs</a></td>
<td id='title'><a href='index.php?word=5'>sosdfsdfmething</a></td>
<td id='title'><a href='index.php?word=6'>sdfsdfsdf</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=7>s</a></td>
<td id='title'><a href='index.php?word=8'>fsdfdsf</a></td>
<td id='title'><a href='index.php?word=9'>dfsdfsdf</a></td>
</tr>
and should be more like:
Code: Select all
<tr>
<td id='title'><a href='index.php?word=1'>foo</a></td>
<td id='title'><a href='index.php?word=4'>foo</a></td>
<td id='title'><a href='index.php?word=7'>foo</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=2'>foo</a></td>
<td id='title'><a href='index.php?word=5'>foo</a></td>
<td id='title'><a href='index.php?word=8'>foo</a></td>
</tr>
<tr>
<td id='title'><a href='index.php?word=3>foo</a></td>
<td id='title'><a href='index.php?word=6'>foo</a></td>
<td id='title'><a href='index.php?word=9'>foo</a></td>
</tr>
you wonder why I didn't get it
Here's the solution:
Code: Select all
<?php
function numberSequence($start, $cols, $end)
{
$rows = ($end - $start) / $cols;
$out = array();
for ($i = 0; $i < $rows; ++$i) {
$row = array();
for ($j = 0; $j < $cols; ++$j) {
$rowElement = $i + ($cols * $j) + $start;
if ($rowElement >= $end) {
break;
}
$row[] = $rowElement;
}
$out[] = $row;
}
return $out;
}
attest(numberSequence(0, 1, 3), array(array(0), array(1), array(2)));
attest(numberSequence(1, 1, 3), array(array(1), array(2)));
attest(numberSequence(10, 1, 10), array());
attest(numberSequence(0, 2, 4), array(array(0, 2), array(1, 3)));
attest(numberSequence(1, 2, 4), array(array(1, 3), array(2)));
attest(numberSequence(1, 3, 10), array(array(1, 4, 7), array(2, 5, , array(3, 6, 9)));
function attest($actual, $expected)
{
if ($actual === $expected) {
return;
}
// yeah i know it's deprecated mark-up
echo '<table><tr>';
echo '<td bgcolor="eeeeee" valign="top"><pre>';
var_dump($actual);
echo '</pre></td><td valign="top"><pre>';
var_dump($expected);
echo '</pre></td>';
echo '</tr></table>';
}
If you run it and you get no output, it worked.
That was pretty tough though.
Posted: Sun Jul 29, 2007 8:46 am
by spamyboy
Well sorry for bad example, all in all I'm glad that you undertood what I ment and helped, thank you.
Posted: Sun Jul 29, 2007 11:06 am
by Ollie Saunders
It took me about 30 minutes to get that right. Few would bother helping to that extent.
Posted: Mon Jul 30, 2007 7:12 am
by spamyboy
Ok, it's probaly not my day.
After spending 3 hours trying to understand how can I use this with my query I got up with no results, pleas explain.
Posted: Mon Jul 30, 2007 7:35 am
by Ollie Saunders
Firstly change the name from numberSequence() to rowsToColsIndexGenerator() or something, I didn't give the name any proper thought until now. Here's an untested usage:
Code: Select all
function getAllRows($result)
{
if (!is_resource($result)) {
return false;
}
$rows = array();
while ($row = mysql_fetch_object($result)) {
$rows[] = $row;
}
return $rows;
}
$rows = getAllRows($result);
foreach (rowsToColsIndexGenerator(0, 3, count($rows)) as $rowIndexes) {
echo '<tr>';
foreach ($rowIndexes as $index) {
$cell = $rows[$index];
echo '<td><a href="index.php?word=' . $cell->id . '">' . $cell->word . '</a></td>';
}
echo '</tr>';
}
Posted: Mon Jul 30, 2007 7:49 am
by spamyboy
It look's great, thank you wery mutch.