NameSilo

PHP/SQL - While.. Help Please.

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

LogiK

Established Member
Impact
11
Ok, I Am Building My Own Arcade Script.
(Not To Resell Just For My Own Use)

I Know How To Echo Out Game ect.
PHP:
$query=mysql_query("SELECT * FROM games ORDER by id DESC");
<? while($att = mysql_fetch_object($query)){ ?>
Then My HTML Here
<? } ?>

And That Would Echo Out Like:
Game1
Game2
Game3
Game4

But What I Want To Know How To Do Is Change It So That It Echos It Out Differently Like:
Game1 - Game2
Game3 - Game4

So It Has 2 Games Per Line.

Anyone Help?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Simple once someone shows you :P

If you just want a dash as your post indicates:
PHP:
<?php

$i = 0;

while( condition )
{
	echo ($i & 1) ? '-' : null;

	// Your bulky html here
	
	echo ($i & 1) ? '<br />' : null;
	$i++;
}
?>

If you want a table then the theory is the same but just with TR and TD.
 
Last edited:
1
Close
PHP:
<?php
$query=mysql_query("SELECT * FROM games ORDER by id DESC"); 
$c = 0;
while($att = mysql_fetch_assoc($query)){ 
if($c == 0) { echo $att['gamename']." - "; $c++;  }
else { echo $att['gamename']."<br />"; $c = 0; }
} 
?>
 
0
Close
bliss, since we are now handing people things on plates :)

PHP:
<?php
$query = mysql_query("SELECT * FROM games ORDER by id DESC"); 
$c = 0;

while( $att = mysql_fetch_assoc( $query ) ) 
{ 

	echo ($c & 1) ? " - {$att['gamename']}<br />" : $att['gamename'];
	
	/*
		if($i & 1) 
		{
			echo "- {$att['gamename']}<br />";
		}
		else
		{
			echo $att['gamename'];
		} 
	*/
	$c++;
} 
?>

A little cleaner than the 2 conditionals :)
 
Last edited:
0
Close
What exactly does the & do in if ($i & 1)?
I normally do if ($i % 2) or something.
 
0
Close
Dan said:
What exactly does the & do in if ($i & 1)?
I normally do if ($i % 2) or something.

Ahhh, will never get any revision done :hehe:
Using PHP's bitwise And operator (&), we can check to see if the 1-bit is on or off. If it's on, we have an odd number, otherwise, an even number.
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back