Log in

View Full Version : How to use foreach to display data from multiple column?



devil_vin
09-20-2007, 08:23 AM
Hi..guys! I have a form and use foreach to output value from multiple column in database.What wrong went in the following script?I can only get the movie name but not category as well.How about if category likely to be bracketed?




<tr>
<td>
<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query("SELECT name,category FROM $tbl_name")
or die("Cannot execute query.");

$numrow = mysql_num_rows($result);

if ($numrow != 0) {
for ($counter = 0; $row = mysql_fetch_row($result); $counter++) {
foreach ($row as $key => $value) {

if ($key == 0) {
?>
<strong><br>
<?php echo $value;?></strong></font></td>
</tr>
<?
}
}
}
}
}
?>

boogyman
09-20-2007, 12:37 PM
if ($key == 0) {
?>
<strong><br>
<?php echo $value;?></strong></font></td>
</tr>
<?
}

this wont print out anything but the first row. you dont want to check if the key is 0. and you are also only printing the value, not the key. to print both you could use somethign like



if() {?>
<tr>
<th><?php echo $key ?></th>
<td><?php echo $value ?></td>
</tr>
<?php } ?>

devil_vin
09-20-2007, 02:00 PM
I am using mysql_fetch_array() rather than mysql_fetch_rows().



<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query("SELECT name,category FROM $tbl_name")
or die("Cannot execute query.");

$numrow = mysql_num_rows($result);

if ($numrow != 0) {
while($rows = mysql_fetch_array($result)){
echo '<strong><br>' . $rows[0] .' (' . $rows[1] .') </strong>';
}

?>



Let said now I have seven rows of record need to be fetched out,how can I write a single SQL statement to query them out and display properly,I have an auto-increment id column in the table.Thanks...

boogyman
09-20-2007, 03:05 PM
$row[0] tells you very little as to what the key is for this item.
mysql_fetch_array() allows you to reference by key, rather than row.


if ($numrow != 0) {
while($rows = mysql_fetch_array($result)){?>
<tr>
<th><?php echo $rows[$key]; ?></th>
<td><?php echo $rows[$value]; ?></td>
</tr><?php
}



now I have seven rows of record need to be fetched out
that is what the WHERE clause is for


$result = mysql_query("SELECT name,category FROM $tbl_name WHERE _____")
or die("Cannot execute query.");

devil_vin
09-20-2007, 03:27 PM
Is there any problem with using index.$row[0] is name;$row[1] is category.
I have a query statement to display all seven recordsbut why the first record is skipped?



$result = mysql_query("SELECT name,category FROM $tbl_name
WHERE id BETWEEN '1' AND '7'")
or die("Cannot execute query.");

boogyman
09-20-2007, 04:03 PM
WHERE id<8

Twey
09-20-2007, 04:43 PM
You can't always guarantee that the first seven IDs will be below 8; for example, if a row is deleted the eighth row will have an ID of 8. Instead, use a LIMIT clause:
$result = mysql_query(sprintf('SELECT name, category FROM %s LIMIT 8', $tbl_name))
or die('Cannot execute query.');

devil_vin
09-21-2007, 05:48 AM
ok.by this moment how can I align all screening time in a single row?



<table width="100%" height="47" border="0">
<tr>
<td height="21" colspan="6"><font face="Arial">------------------------------------------------------------------------------------------------------------------------------ <br>

<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
LIMIT 1', $tbl_name)) or die('Cannot execute query.');

//$numrow = mysql_num_rows($result);


while ($rows = mysql_fetch_assoc($result)) {
echo '<strong>' . $rows['name'] . ' (' . $rows['category'] . ')
<br></strong></font>';
foreach (explode(',', $rows['screeningTime']) as $time) { ?>
<label>
<td><input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?></td>
</label>
</td></tr>
<?php } ?>
<?
}


}
?>

Twey
09-21-2007, 07:38 AM
Remove the wrapping <tr> element.

devil_vin
09-21-2007, 02:22 PM
Thanks for reply.Now the entire thing,including the dashed line is on center,how can keep it on left?




<table width="100%" border="0">
<tr>
<td height="68">
<table width="100%" height="47" border="0">

<font face="Arial">------------------------------------------------------------------------------------------------------------------------------ <br>

<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
LIMIT 1', $tbl_name)) or die('Cannot execute query.');

//$numrow = mysql_num_rows($result);


while ($rows = mysql_fetch_assoc($result)) {
echo '<strong>' . $rows['name'] . ' (' . $rows['category'] . ')
<br></strong></font>';
foreach (explode(',', $rows['screeningTime']) as $time) { ?>
<label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>
</label>

<?php } ?>
<?
}


}
?>

boogyman
09-21-2007, 02:41 PM
1.

<font face="Arial">------------------------------------------------------------------------------------------------------------------------------ <br>
a. get rid of the font tag <font> it has been depreciated.
b. you can use the horizontal rule tag and kill 2 birds with 1 stone.

<hr>

2.

Thanks for reply.Now the entire thing,including the dashed line is on center,how can keep it on left?

if the table is 100% there shouldn't be any center but you can use css styling to accomplish this, and force everything to the left


<style type="text/css">
table, table * {
margin: 0;
padding: 0;
text-align: left;
}
</style>

devil_vin
09-21-2007, 04:41 PM
Thanks for helping,by now I would like to submit the form after clicking one of the radio button.How can the selected screeningTime and its movie name being captured by $_POST?Really appreciate for your initiative.



<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
LIMIT 7', $tbl_name)) or die('Cannot execute query.');

//$numrow = mysql_num_rows($result);


while ($rows = mysql_fetch_assoc($result)) {
echo '<table width="100%" border="0"><tr><td height="68">
<table width="100%" height="47" border="0">
---------------------------------------------------------------------------------------------------------<br>';

echo '<strong>' . $rows['name'] . ' (' . $rows['category'] . ')
<br></strong>';
foreach (explode(',', $rows['screeningTime']) as $time) { ?>
<label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>&nbsp;&nbsp;&nbsp;
</label>
<?php } ?>
<?
}


}
?>

boogyman
09-21-2007, 05:20 PM
process.php


$_POST['movie'];
$_POST['time'];


file.html


<form name="something" action="process.php" method="post">
<label>Movie</lable>
300<input type="radio" name="movie" value="300">
We Were Soldiers<input type="radio" name="movie" value="We were Soldiers">
Titanic<input type="radio" name="movie" value="Titanic">
...
<label>Time</lable>
11:00am<input type="radio" name="time" value="1100">
7:10pm<input type="radio" name="time" value="1910">
9:15pm<input type="radio" name="time" value="2115">
...
</form>


but I thought you were grabbing your information from the database?

devil_vin
09-21-2007, 05:32 PM
Ya.What I have done is to grab movie list from database.But now user need to register movie ticket so the selected screeningTime and its movie title need to be captured and stored to database later.

devil_vin
09-22-2007, 09:13 AM
Did anybody know how to capture it?Thanks for your generious help...