Mysql table listing (php logic)

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

Post Reply
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Mysql table listing (php logic)

Post 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:

Code: Select all

http://www.anglu-lietuviu.com/A/
Pleas give example on how to di this.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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>';
}
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Both of your examples are different.

Read the first two links in Useful Posts.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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 :roll:

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.
Last edited by Ollie Saunders on Mon Jul 30, 2007 7:37 am, edited 2 times in total.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Well sorry for bad example, all in all I'm glad that you undertood what I ment and helped, thank you.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

It took me about 30 minutes to get that right. Few would bother helping to that extent.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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>';
}
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

It look's great, thank you wery mutch.
Post Reply