Lifelesspeople.com

 Forum FAQsForum FAQs  Knowledge BaseKnowledge Base  RulesRules   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   HostingHosting   RegisterRegister 
 DonateDonate   WikiWiki   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

php, mysql and a table

 
Lifelesspeople.com Forum Index -> Web Architects' Abode
Post new topic   Reply to topic View previous topic :: View next topic  
Author Message
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2984
Location: Earth

PostPosted: Fri Nov 23, 2007 2:50 pm    Post subject: php, mysql and a table Reply with quote

i'm trying to get the values of a database inserted into a table to display on a webpage

Theres 10 results, and i'm trying to get it so that they display 5 on a line, and under each 5, there is a check box

So its like

Image1 || Image2 || Image3 || Image4 || Image5
Check1 || Check2 || Check3 || Check4 || Check5
Image6 || Image7 || Image8 || Image9 || Image10
Check6 || Check7 || Check8 || Check9 || Check10

but i'm having trouble
I can get them to appear all on 1 line, not 2, and the check boxes aren't appearing at all

I know i'm obviously doing something wrong, if i weren't, it'd work

Code:
    <table width="110" border="1">
      <tr>
        <?php
    $sql = mysql_query("SELECT * from `game_mall` where date <= '" . date("Y-m-d") . "' AND type='char'") or die(mysql_error());
      $result = mysql_num_rows($sql);
      if ($result == 0) {
         die("<br /><strong>Error getting game data</strong>");
      } else {
         while ($row = mysql_fetch_array($sql)) { ?>
        <td width="100"><?php echo "<img src=\"{$row[url]}\" alt=\"{$row['alttext']}\"/>" ; ?></td><?php  } ?>
      </tr>
      <tr> <?php while ($row = mysql_fetch_array($sql)) { ?>
        <td><input type="checkbox" name="<?php print $row['alttext']; ?>" value="checkbox" /></td>
        <?php } } ?>
      </tr>    
    </table>


webpage: http://www.darknesswithin.com.ru/games/mall.php
_________________
Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Caretaker - A Star Trek Voyager TCG
Back to top
 
krt
...


Joined: 11 Jan 2005
Posts: 4704
Location: Down Under

PostPosted: Fri Nov 23, 2007 4:52 pm    Post subject: Reply with quote

You can't have two of the following in succession:
Code:
while ($row = mysql_fetch_array($sql))


A crude solution is to use mysql_data_seek() in between to reset the position.

Or you can have a temporary array with the alttext values in the first loop and use that for the second one.
Back to top
 
packagedeliverer
Pro Freelancer::llp elder


Joined: 20 Feb 2004
Posts: 2692
Location: belgium

PostPosted: Sat Nov 24, 2007 9:37 pm    Post subject: Reply with quote

How about:

Code:
       <table width="110" border="1">
      <tr>
        <?php
       $sql = mysql_query("SELECT * from `game_mall` where date <= '" . date("Y-m-d") . "' AND type='char'") or die(mysql_error());
      $result = mysql_num_rows($sql);
           if ($result == 0) {
         die("<br /><strong>Error getting game data</strong>");
      }else {
     $teller = 1;
     $table = "";
         while ($row = mysql_fetch_array($sql)) {
      
       $line_one = "<td width=\"100\"><img src=\"{$row[url]}\" alt=\"{$row['alttext']}\"/></td>\n";
       $line_two = "<td><input type=\"checkbox\" name=\"{$row['alttext']}\" value=\"checkbox\" /></td>\n";

      if($teller == 5){
         $table .= $line_one . "\n</tr><tr>\n" . $line_two;
      }
      
      $teller++;
      
      } ?>
        </tr>   
    </table>


I haven't checked the code for errors though, but might give a clue Smile
So basicly what it does, it fills the rows up (each pair of cells of both rows at the time) and concatenates them after 5 columns.
Should work
_________________
[img:0a108d93e8]http://img338.imageshack.us/img338/9430/packagedeliverer54hy.gif[/img:0a108d93e8]
Back to top
 
LP-SolidRaven
Dictator of the Dump


Joined: 06 Jun 2004
Posts: 7173
Location: The cheese is made out of moon

PostPosted: Sun Nov 25, 2007 4:11 am    Post subject: Reply with quote

shouldn't it be $line_one .= instead of $line_one =
Cause now you'll probably overwrite it.
_________________
Quote:

<bart416> I just realized something
<bart416> we celebrate the fact that this piece of rock made one rotation around a glowing ball of plasma that is kept together due to its own gravity well
<njsg> HAPPY NEW YEAR
<Easter> ^^
Back to top
 
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2984
Location: Earth

PostPosted: Sun Nov 25, 2007 4:16 am    Post subject: Reply with quote

i've tried doing it both ways, but either way doesn't show anything up

If i look at the source, i just get
Quote:
<table width="110" border="1">
<tr>
</tr>
</table>


The code also seems to be missing a }

I've actually managed to get it to work, but i'd prefer it to be done the way suggested on here since mine involves having both the image and the checkbox in the same box, which i dont want
_________________
Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Caretaker - A Star Trek Voyager TCG
Back to top
 
nooc
Master Poster


Joined: 10 Aug 2005
Posts: 215


PostPosted: Mon Nov 26, 2007 8:00 am    Post subject: Reply with quote

You need to buffer the table rows untill you need to break a row, before you print them in order to have the image and checkbox on different table rows..

Code:
<table border="1">
<?php

$result = mysql_query("SELECT * from `game_mall` where date <= '" . date("Y-m-d") . "' AND type='char'") or die(mysql_error());

$cnt=0;
$cntN = mysql_num_rows();
$line_one = $line_two = '';
while (false!==($row = mysql_fetch_array($result))) {

   $line_one .= "  <td width=\"100\"><img src=\"{$row['url']}\" alt=\"{$row['alttext']}\"/></td>\n";
   $line_two .= "  <td><input type=\"checkbox\" name=\"{$row['alttext']}\" value=\"checkbox\" /></td>\n";
   
   if((++$cnt % 5)==0 or $cnt==$cntN) {
   
      echo " <tr>\n$line_one </tr>\n <tr>\n$line_two </tr>\n";
      $line_one = $line_two = '';
   }
}

?></table>
Back to top
 
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2984
Location: Earth

PostPosted: Mon Nov 26, 2007 9:23 am    Post subject: Reply with quote

that seems to work, but i get this error, as well as the images/checkboxes showing

Quote:
Warning: Wrong parameter count for mysql_num_rows() in /usr/home/desbrina/public_html/games/mall.php on line 164


Line 164 is this line
Code:
$cntN = mysql_num_rows();


Edit: Sorted the error
_________________
Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Caretaker - A Star Trek Voyager TCG
Back to top
 
packagedeliverer
Pro Freelancer::llp elder


Joined: 20 Feb 2004
Posts: 2692
Location: belgium

PostPosted: Wed Nov 28, 2007 6:59 am    Post subject: Reply with quote

SolidRaven wrote:
shouldn't it be $line_one .= instead of $line_one =
Cause now you'll probably overwrite it.


Note to self:
Note wrote:
Check the code first, even if it is just to give some one a clue, or everyone will be picking nits about it


Evil or Very Mad
_________________
[img:0a108d93e8]http://img338.imageshack.us/img338/9430/packagedeliverer54hy.gif[/img:0a108d93e8]
Back to top
 
Display posts from previous:   
Post new topic   Reply to topic    Lifelesspeople.com Forum Index -> Web Architects' Abode All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Home | Hosting | News | Forum | Links | System Status | About | Archive | Donate ]
Powered by phpBB © 2001, 2002 phpBB Group
All trademarks and copyrights on this page are owned by their respective owners. Posts and comments are owned by the poster. Everything else © 2001 - 2007 Lifelesspeople.com