General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 23rd, 2004, 06:04 PM
omgwtf omgwtf is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 4 omgwtf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face Formating sql result (in table)

Hi!
I would like to know how I can format my mysql results into a nice little table.

PHP Code:
<?php
require_once('db.php');
function 
fun() {
global 
$db$tableusers;
$result mysql_query("SELECT user_nickname, user_bio, id FROM $tableusers");
while(list(
$user$bio$wtfid) = mysql_fetch_row($result)) {
 if (empty(
$bio)) {
  { }
 } else {

  echo 
"$bio<br>\n$user";
 }
}
}

?>


The results should be spit out in this:
(where name is $user and bio is $bio)
Code:
<table>
  <tr>
	<td>Name<br>
	Bio</td>
	<td>Name (Diffrent user)<br>
	Bio (Diffrent user)</td>
	<td>ect</td>
	<td>ect</td>
  </tr>
  <tr>
	<td>ect</td>
	<td>ect</td>
	<td>ect</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
</table>


How could I do this? How would it look code-wise?

thanks in advanced. Im pretty new to all this.

Reply With Quote
  #2  
Old March 24th, 2004, 08:09 AM
tobycloud tobycloud is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Near Albany NY
Posts: 27 tobycloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to tobycloud Send a message via AIM to tobycloud
Hope this helps

Quote:
Code:
<table>
  <tr>
	<td>Name<br>
	Bio</td>
	<td>Name (Diffrent user)<br>
	Bio (Diffrent user)</td>
	<td>ect</td>
	<td>ect</td>
  </tr>
  <tr.............



There are two ways of doing this:
Solution 1
Code:
<table>
  <tr>
	<td><?php echo $user; ?><br>
	<?php echo $bio; ?></td>
	<td>Name (Diffrent user)<br>
	Bio (Diffrent user)</td>
	<td>ect</td>
	<td>ect</td>
  </tr>
  <tr.............


Solution 2
Code:
<?php
echo "
  <table>
    <tr>
  	<td>$user<br>
  	$bio</td>
	<td>Name (Diffrent user)<br>
	Bio (Diffrent user)</td>
	<td>ect</td>
	<td>ect</td>
  </tr>
  <tr.............
";
?>


Be advised when using the second solution certain characters will need to be formatted differently within the echo. ie. (" will be \"). Unless you're familiar with PHP I would use the first solution. Check out php.net documentation for any questions.

Reply With Quote
  #3  
Old March 24th, 2004, 02:17 PM
omgwtf omgwtf is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 4 omgwtf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

But, wouldnt it be something like
foreach $user + $bio; echo <td>$user <br> $bio</td>

Reply With Quote
  #4  
Old March 24th, 2004, 03:54 PM
tobycloud tobycloud is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Near Albany NY
Posts: 27 tobycloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to tobycloud Send a message via AIM to tobycloud
Quote:
Originally Posted by omgwtf
Thanks

But, wouldnt it be something like
foreach $user + $bio; echo <td>$user <br> $bio</td>

No, whitespace is irrelevant

Reply With Quote
  #5  
Old March 24th, 2004, 06:20 PM
omgwtf omgwtf is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 4 omgwtf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thank you, thank you, thank you!!

For now it works. I still need to do some testing, but with three
Names + Bios are already up and running this way.

How could I insert the next 4 results into a new row?

(sorry if im being a very very annoying newbie)

Reply With Quote
  #6  
Old March 24th, 2004, 10:07 PM
tobycloud tobycloud is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Near Albany NY
Posts: 27 tobycloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to tobycloud Send a message via AIM to tobycloud
Quote:
Originally Posted by omgwtf

How could I insert the next 4 results into a new row?
(sorry if im being a very very annoying newbie)

It's Ok, everyone is a n00b at some point. Try this:

PHP Code:
echo "<table>";
 
while (
$myrow mysql_fetch_array($result)) {
$user $myrow["user"];
$bio $myrow["bio"];
 
echo 
"
  <tr>
    <td>
      $user<br>
      $bio
    </td>
  </tr>
"
;
}
 
echo 
"</table>"


You will need to adapt this to your own needs, but that script will create a table. In the table it will create as many rows as needed, depending on how many different rows are returned from your query.

Reply With Quote
  #7  
Old March 27th, 2004, 03:40 PM
omgwtf omgwtf is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 4 omgwtf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks everyone!
It still doesnt quite work, but heres what I have till now. Its not doing anything, but I believe it would work. ...If I would know what I'm doing wrong:

PHP Code:
<?php
echo "<table WIDTH=100%>";
$result mysql_query("SELECT user_nickname, user_bio, id FROM $tableusers");
while (
$myrow mysql_fetch_array($result)) {
  
$user $myrow["user_nickname"];
  
$bio$myrow["user_bio"];  
  if (empty(
$bio)) {
  { }
  } else {
    echo 
"$bio";
    foreach (
$result as $count)
    
$count++; 
    if (
$count>5)  { echo "</tr><tr>"$count=0; }
    }
  }
}
 
echo 
"</table>";
?>




Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Formating sql result (in table)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT