Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > PHP Programming
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 02-10-2003, 02:59 AM
  #1
jollyfactory
Deity (Level 17)
 
jollyfactory's Avatar
 
Join Date: Nov 2000
Location: Sydney, Australia
Posts: 775
iTrader: (0)
jollyfactory is on a distinguished road
PHP: show two array items in while loop

Just wondering if this is possible. First I have this code

PHP Code:
<?php
mysql_connect
("localhost","root","**");

//get all main page categories
$sql mysql_query("SELECT * FROM table");

?>
<table border=1 width=100%>
<?
while ($row mysql_fetch_array($sql)) {
        
//place items into variables
    
$title $row['title'];
    
$description $row['description'];
    
?>
    <tr><td><?echo($title);?></td></tr>
    <tr><td><?echo($description);?></td></tr>
<tr><td>=========================</td></tr>    
<?
    
    
}
?>
</table>
As you can see, it will keep looping through and printing a new table row until the array left in $row is empty.

The link below is how it would sort of output the array
http://resma.net/example1.html

And this is how I want it to look
http://resma.net/example.html
As you can see, the output is the same, its just that each output of the loop is placed in a one of two cells. Just wondering how I could achieve this.

Any help is appreciated.

If you need more clarification, just ask and I'll try to be more clearer.

Thanks
__________________

The website said: Please Use Internet Explorer 6 or better for our site ... so I used Netscape
jollyfactory is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-10-2003, 09:54 AM
  #2
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,868
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
actually theres is a bunch of ways to do that. here is one
PHP Code:
<?php
mysql_connect
("localhost","root","**");

//get all main page categories
$sql mysql_query("SELECT * FROM table");

$i 0;
echo
"<table border=\"1\" width=\"100%\">\n";
echo
"<tr>\n";
echo
"<td>\n";
while (
$row mysql_fetch_array($sql)) {
    
//place items into variables
    
$title $row['title'];
    
$description $row['description'];
    echo
"<table>\n";
    echo
"<tr><td>($title)</td></tr>\n";
    echo
"<tr><td>($description)</td></tr>\n";
    echo
"<tr><td>=========================</td></tr></table>\n";
if (
$i%2){
    echo
"</td>\n";
    echo
"<td>\n";
} else {
    echo
"</td></tr>\n";
    echo
"<tr><td>\n";
}
$i++;
}
?>
</table>
I think that will work so give it a try. it is untested too.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-10-2003, 11:23 PM
  #3
jollyfactory
Deity (Level 17)
 
jollyfactory's Avatar
 
Join Date: Nov 2000
Location: Sydney, Australia
Posts: 775
iTrader: (0)
jollyfactory is on a distinguished road
thankyou so much scoutt!!

I would have never figured this out myself scoutt so thankyou Your code is great. Thanks for cleaning up my code too (I'm a lazy programmer). I was just wondering though why you used

echo("<html code here> $variable");

instead of
?>
<html code> <?echo($variable);?>
<?

Well your code is excellent and worked great.. just had to change
PHP Code:
if ($i%2){ 
to
PHP Code:
if (!($i%2) ){ 
Thanks You rock
__________________

The website said: Please Use Internet Explorer 6 or better for our site ... so I used Netscape
jollyfactory is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-10-2003, 11:31 PM
  #4
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,868
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
you're welcome glad I could help.

the reason why I put html in with php is becasue. tha tis just the way I do it, personal preference I suppose and if you write php thne why not, why insert the <? all the time so it goes in and out of php, just make it all php.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-11-2003, 12:42 AM
  #5
jollyfactory
Deity (Level 17)
 
jollyfactory's Avatar
 
Join Date: Nov 2000
Location: Sydney, Australia
Posts: 775
iTrader: (0)
jollyfactory is on a distinguished road
I see I guess I'm just too lazy to do that then lol. I think I'll start coding your way though because it looks much more cleaner.
__________________

The website said: Please Use Internet Explorer 6 or better for our site ... so I used Netscape
jollyfactory is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-11-2003, 08:13 AM
  #6
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 30,868
iTrader: (0)
scoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the roughscoutt is a jewel in the rough
yeah that is the worst thing for a programmer, being lazy. when you are lazy it makes the code sloppy and then it is harder to find the errors which when you are sloppy are a lot.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart



 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 07:15 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.