Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
MySQL Data Formatting (in tables)
Old 05-06-2005, 07:21 PM MySQL Data Formatting (in tables)
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
Alright, I've got a reviews page, and here is what I'm looking to do:

- the table will have a header (the console), and each and every game that belongs to that console will be there (the one I currently have creates a new header for each one, so it has 2 different Xbox headers)
- the games will be ordered by name alphabetically (ascending)

Can someone help me get started with this?

(and also, if it isn't too much trouble, how do you deal with multiple pages? Like, say, on my comments page I made for my blog, I have a "how many comments?" option (which does nothing at the moment), and I want to it display that many comments per page... how do I make seperate pages?)
Bloodsyne is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-06-2005, 07:39 PM
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
Have you used any sort of loops with PHP yet?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 05-06-2005, 07:43 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
I did use one with the alternating table colors... so, yes, I have.
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Old 05-06-2005, 07:53 PM
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
The principle is all right there then. Basically, from what I understand, your headers would be static so you can create the table and TD's etc, in html, then where you want the repeat data from the database to be, just loop over an array from the db.

Here is an example taken straight from the php.net manual page for the mysql functions...

PHP Code:
<?php
// Connecting, selecting database
$link mysql_connect('mysql_host''mysql_user''mysql_password')
   or die(
'Could not connect: ' mysql_error());
echo 
'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query 'SELECT * FROM my_table';
$result mysql_query($query) or die('Query failed: ' mysql_error());

// Printing results in HTML
echo "<table>\n";
while (
$line mysql_fetch_array($resultMYSQL_ASSOC)) {
   echo 
"\t<tr>\n";
   foreach (
$line as $col_value) {
       echo 
"\t\t<td>$col_value</td>\n";
   }
   echo 
"\t</tr>\n";
}
echo 
"</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
That is fairly close to what you want to do.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 05-06-2005, 08:08 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
Alright, yeah, I'm getting the basic concept of it, but...

When I try that, it gives me some strange results. And aside from that, I want to have the console name at the top... well, here's a basic model of what I want...

Code:
XBOX
------
game 1
game 2
game 3

GAMECUBE
----
game 1
game 2
game 3
That code above displays it like this:

Code:
XBOX
Game Name
1 (the id)
And repeats for each. It may be that I'm doing something wrong, but... I think I'm missing something here.
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Old 05-06-2005, 08:54 PM
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
Ok, here is an example that I worked up.

PHP Code:
<table width="500" border="1" cellspacing="0" cellpadding="0">
  <tr align="center">
    <td colspan="2">Xbox</td>
  </tr>
  <tr align="center">
      <?php
          
// assume connection is present above
        
        // lets get the data from the db
        
$sql "SELECT gameid,gamename FROM tblXbox ORDER BY gamename ASC";
        
$query mysql_query($sql);
        
        
// Now lets loop over the provided array
        // and echo the results
        
while($res mysql_fetch_assoc($query) {
            echo 
"<td>$res[gamename]</td>";
            echo 
"<td>$res[gameid]</td>";
        }
    
?>
  </tr>
</table>

<table width="500" border="1" cellspacing="0" cellpadding="0">
  <tr align="center">
    <td colspan="2">Gamecube</td>
  </tr>
  <tr align="center">
      <?php
          
// assume connection is present above
        
        // lets get the data from the db
        
$sql "SELECT gameid,gamename FROM tblGamecube ORDER BY gamename ASC";
        
$query mysql_query($sql);
        
        
// Now lets loop over the provided array
        // and echo the results
        
while($res mysql_fetch_assoc($query) {
            echo 
"<td>$res[gamename]</td>";
            echo 
"<td>$res[gameid]</td>";
        }
    
?>
  </tr>
</table>
As you can see, all your really doing is echoing out new td's each loop so that it will dynamically add as many as there are games, putting them in any order is as simple as moving the order within the echo function. If you wanted to add more columns though you would have to change the column span of the first set of td's to match.

Also, I'm not sure how familiar you are with SQL but as you can see from the end of that query MySQL will automatically return the results in alphabetical order, according to the gamename column, ascending.

Let me know if you need more help.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 05-07-2005, 01:57 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
Alright, I suppose with that approach it'd have been a lot easier, but I had just one MySQL table for all reviews, and just a field that said which console it belonged to... I may just have to do it the way you have demonstrated, though.

As for the alphabetical order.. yeah, I'm thinking I could've figured that out if I hadn't been ordering them by which console they were in.. I guess I was just going about it the wrong way.

I'll try it out tonight.

Thanks for the help.
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL Data Formatting (in tables)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.02813 seconds with 12 queries