0

while ($row = mysql_fetch_array($result))

Is nice command which iterates through every line, if $result is a MySQL query. It iterates every single line, one by one. See, I've got situation like:

<tr>
    <td>Source 1</td>
</tr>
<tr>
    <td>Source 2</td>
</tr>

And it is kind of part of design, I can't really change the order or mix this up. Is it possible to call ID's 1 2---3 4---5 6 in place of 1-2-3-4-5-6?

Edited by RikTelner

3
Contributors
9
Replies
32
Views
2 Years
Discussion Span
Last Post by RikTelner
0

You can do whatever you want. You control your loop. Keep a counter to know your current position. Can you show your expected output in a little more detail?

0
<table>
    <tr>
        <td>Source 1</td>
    </tr>
    <tr>
        <td>Source 2</td>
    </tr>
</table>
<table>
    <tr>
        <td>Source 3</td>
    </tr>
    <tr>
        <td>Source 4</td>
    </tr>
</table>
<table>
    <tr>
        <td>Source 5</td>
    </tr>
    <tr>
        <td>Source 6</td>
    </tr>
</table>

By using only (I know you can't form PHP and HTML like that):

while ($row = mysql_fetch_array($result)) {
<table>
    <tr>
        <td>Source X</td>
    </tr>
    <tr>
        <td>Source Y</td>
    </tr>
</table>

I know that while() works either whether condition is true or isn't null. I just can't figure on how, I think I could use loop around that, but then you would need 2 scripts which one after another reload the query. This is hard way, which I may know how to do after day or two, but to my expectation there should be mucho moro efficient way.

Edited by RikTelner

2

Seems you want to start a new row on odd numbers. That's pretty simple.

If $i is your loop counter

//LOOP
if($i % 2 !== 0) //if odd number
{
   $table .= "<tr><td>{$row['whatever']}</td>";

}else{
    $table .= "<td>{$row['whatever']}</td></tr>";
}
//END LOOP

There's a bit more to it with regard to the last row (ensuring well formed html), but that's easy, just see if odd number of records. If so just add a blank cell and a close tr tag.

0

I think we misunderstood one another. @diafol you created correct script, to missing situation. I'm willing to get "while" to displaying results in pairs, not one by one. What while does is getting results id=1, id=2, id=3, id=4. But what I'm actually seeking is id=1 and id=2, id=3 and id=4, id=5 and id=6.
I tried to manage that loop, but I messed up my entire website, it doesn't even display anymore... poor me.

@edit - Wait. That actually might be adjustable to work just right.

Edited by RikTelner

0

That doesn't work. Test at the end isn't even displayed. Purely HTML recreation works, the entire website works. But if you try to use PHP and database to it to recreate HTML effect, it fails.

http://pastebin.com/yu6EWq9w

Just don't create spin-offs please. I wish to keep my code little bit private. I'm not feared for someone to use it, I don't want my teacher to able to find this chunk of source code and bind it with my username. Internet is internet, life is life.

Edited by RikTelner

0

Oh dear. $i cannot work with while unless you set it to 0 before the loop and increment it at the end of loop.

$table = "<table>...";
$numRows = //number of rows
$i=0;
while(...)
{
..stuff...

$i++;
}
if($numRows % 2 !== 1)
{
    $table .= "<td>&nbsp;</td></tr>";
}
$table .= "</table>";

Then where you need it...

echo $table;

BTW, please post code here - going to pastebin etc is a pain. AND mysql_* functions have been deprecated. You should not be using them. Go to PDO or mysqli.

0

BTW, please post code here - going to pastebin etc is a pain.

Sorry I can't. You can't delete code from here, but it expires itself from Pastebin.

Go to PDO or mysqli.

You'd be suprised, noobs like me, only support mysql since that's only one I can install. Assumably mysqli is "newer" improved version of mysql and assumeable I'd need to learn new commands. I barely got mysql working don't ask me to get mysqli to work.

1

Why do you need the code to expire? It's not as if it's anything new - this type of code has been posted on daniweb hundreds of times and thousands of times over all the other forums. As Daniweb is a learning resource, we expect visitors to look at our threads and be able to resolve their issues. If you post "vanishing code", you deprive others of this ability and leave threads in a state of disarray.

Mysqli is not "installed" like mysql. It's a part of PHP. mysqli and PDO were introduced in version 5.0 (July 2004). I'm assuming that you're not running PHP4. So you can use mysqli or PDO.

But, up to you. Running obsolete code has its dangers. In addition you are not developing your skills for 2014 and the future.

0

I'm assuming that you're not running PHP4.

php_info();

PHP Version     5.5.9-1ubuntu4.3, I think it's version 5.5.

As Daniweb is a learning resource, we expect visitors to look at our threads and be able to resolve their issues.

Gah, "I don't like this game, but I know how to beat it". The struggle is not about solving a problem within a script, it's about finding proper script to get it to work. HTML + CSS work just fine, just when you ask database and try to automize the process, PHP file turns out to be troll.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.