| Author |
Message |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2984 Location: Earth
|
Posted: Fri Nov 23, 2007 2:50 pm Post subject: php, mysql and a table |
|
|
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
|
Posted: Fri Nov 23, 2007 4:52 pm Post subject: |
|
|
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
|
Posted: Sat Nov 24, 2007 9:37 pm Post subject: |
|
|
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
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
|
Posted: Sun Nov 25, 2007 4:11 am Post subject: |
|
|
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
|
Posted: Sun Nov 25, 2007 4:16 am Post subject: |
|
|
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
|
Posted: Mon Nov 26, 2007 8:00 am Post subject: |
|
|
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
|
|
| Back to top |
|
| |
packagedeliverer Pro Freelancer::llp elder

Joined: 20 Feb 2004 Posts: 2692 Location: belgium
|
Posted: Wed Nov 28, 2007 6:59 am Post subject: |
|
|
| 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 |
 _________________ [img:0a108d93e8]http://img338.imageshack.us/img338/9430/packagedeliverer54hy.gif[/img:0a108d93e8] |
|
| Back to top |
|
| |
|
|
|