Forum Moderators: coopster
I don't have any problems with the searching part or looping of the search to display multiple results. The problem is when I select a checkbox and try to get the results from the 2nd page onto the 3rd. I want the user to be able to select several results from the 2nd page and have them show on the 3rd page.
Here is some of the code from the 2nd (sql result) page :
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);
if ($numberOfRows==0) {
?>
Sorry. No records found !
<?
}
else if ($numberOfRows>0) {
$i=0;
?>
<body>
<form method="POST" action ="show.php">
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="100%">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Add</td>
</tr>
<?
while ($i<$numberOfRows)
{
if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$bed = MYSQL_RESULT($result,$i,"bed");
$bath = MYSQL_RESULT($result,$i,"bath");
$price = MYSQL_RESULT($result,$i,"price");
$address = MYSQL_RESULT($result,$i,"address");
$city = MYSQL_RESULT($result,$i,"city");
$add = '<input type ="checkbox" name="cb[]" value="<? echo $k++; ?>">';
$fName = MYSQL_RESULT($result,$i,"fName");
$lName= MYSQL_RESULT($result,$i,"lName");
$phone= MYSQL_RESULT($result,$i,"phone");
?>
<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName" value="<? echo $fName; ?>" />
<input type="hidden" name="lName" value="<? echo $lName; ?>" />
<input type="hidden" name="phone" value="<? echo $phone; ?>" /></TD>
</TR>
<?
$i++;
} // end while
?>
</TABLE>
<input name="Submit" type="submit" value="Showing Agenda" />
</form>
<?
} // end of if numberOfRows > 0
?>
</body>
And Here is the code from page 3 (show.php):
<body>
<table cellpadding="3" cellspacing="0" width="100%" border="0">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Name</td>
<td>Contact</td>
</tr>
<?
foreach ($_POST['cb'] as $k){
$bed = $_POST['bed']['$k'];
$bath = $_POST['bath']['$k'];
$price = $_POST['price']['$k'];
$address = $_POST['address']['$k'];
$city = $_POST['city']['$k'];
$fName= $_POST['fName']['$k'];
$lName= $_POST['lName']['$k'];
$name= $fName. ' ' . $lName;
$phone= $_POST['phone']['$k'];
?>
<tr>
<TD nowrap><? echo $bed; ?></TD>
<TD nowrap><? echo $bath; ?></TD>
<TD nowrap><? echo $price; ?></TD>
<TD nowrap><? echo $address; ?></TD>
<TD nowrap><? echo $city; ?></TD>
<TD nowrap><? echo $name; ?></TD>
<TD nowrap><? echo $phone; ?></TD>
</tr>
<?
;}
?>
</table>
</body>
Right now its not displaying properly at all. I had it only display the last record before no matter which checkbox was selected. But I need it to display a row for each checkbox selected.
I'm sorry, I am new to PHP and would appreciate any help.
//if they clicked a checkbox
for(all of the checkboxes in your form){
if(isset($_POST['valueOfCheckBox'])){
$_SESSION['valueOfCheckBox'] = true;
}//if
}//for
?>
You need to run that on a submit and run it for every checkbox.
It looks like to me that you are naming your checkboxes on an increment. I would recommend putting in a little more code and naming them with text values (it will make identifying bed, bathrooms, etc. easier if the value is bed)
Now in page 3 you would have to check if the session variables are set
so basically
if($_SESSION['valueOfCheckBox']){
//code to print out that part of the table
}//if
You need to do that for all of your checkboxes again in page 3.
Let me know if you have any problems.
I don't know how I can make the code work for all the possible checkboxes because the number of checkboxes that appear on page 2 is a result from a sql query. So I tried incremented and looping the variables from page 2.
Like I said in my first post, I am new to PHP, so please forgive me for being ignorant. But I tried your code and I got a parsing error. So I guess I couldn't understand what you were trying to say with some of it. Sorry for being a n00b lol.
Can I post a link to the site on this forum? I don't wanna break any rules. Maybe if you saw the whole thing it might make more sense to you what I am trying to do.
Thanks again for your reply, I still am gonna try to mess around for a little bit with what you wrote.
<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName" value="<? echo $fName; ?>" />
<input type="hidden" name="lName" value="<? echo $lName; ?>" />
<input type="hidden" name="phone" value="<? echo $phone; ?>" /></TD>
</TR>
I think this should work.
<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed[<?= $k; ?>]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath[<?= $k; ?>]" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price[<?= $k; ?>]" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address[<?= $k; ?>]" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city[<?= $k; ?>]" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName[<?= $k; ?>]" value="<? echo $fName; ?>" />
<input type="hidden" name="lName[<?= $k; ?>]" value="<? echo $lName; ?>" />
<input type="hidden" name="phone[<?= $k; ?>]" value="<? echo $phone; ?>" /></TD>
</TR>
<?
foreach ($_POST['cb'] as $k){
$bed = $_POST['bed']['$k'];
$bath = $_POST['bath']['$k'];
$price = $_POST['price']['$k'];
$address = $_POST['address']['$k'];
$city = $_POST['city']['$k'];
$fName= $_POST['fName']['$k'];
$lName= $_POST['lName']['$k'];
$name= $fName. ' ' . $lName;
$phone= $_POST['phone']['$k'];
?>
$add = '<input type ="checkbox" name="cb[]" value="<? echo $k++; ?>">';
This Lines Have Syntax error for php. Actually not error but it will not print the value of $k. It will be zero so you need to fix that.
<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
And input name for that case must be array. Like the below one
<TD nowrap><input type="hidden" name="bed[// Here u have to use some value]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
Think you will understand for more clarification use the below code-------------------------------
<?
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);
if ($numberOfRows==0) {
?>
Sorry. No records found !
<?
}
else if ($numberOfRows>0) {
$i=0;
?>
<body>
<form method="POST" action ="show.php">
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="100%">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Add</td>
</tr>
<?
while ($i<$numberOfRows)
{
if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$bed = MYSQL_RESULT($result,$i,"bed");
$bath = MYSQL_RESULT($result,$i,"bath");
$price = MYSQL_RESULT($result,$i,"price");
$address = MYSQL_RESULT($result,$i,"address");
$city = MYSQL_RESULT($result,$i,"city");
$add = '<input type ="checkbox" name="cb['.$i.']" value="'.$i.'">';
$fName = MYSQL_RESULT($result,$i,"fName");
$lName= MYSQL_RESULT($result,$i,"lName");
$phone= MYSQL_RESULT($result,$i,"phone");
?>
<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed[<?=$i;?>]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath[<?=$i;?>]" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price[<?=$i;?>]" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address[<?=$i;?>]" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city[<?=$i;?>]" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName[<?=$i;?>]" value="<? echo $fName; ?>" />
<input type="hidden" name="lName[<?=$i;?>]" value="<? echo $lName; ?>" />
<input type="hidden" name="phone[<?=$i;?>]" value="<? echo $phone; ?>" /></TD>
</TR>
<?
$i++;
} // end while
?>
</TABLE>
<input name="Submit" type="submit" value="Showing Agenda" />
</form>
<?
} // end of if numberOfRows > 0
?>
</body>
and the above one should work if u dunt make any mistake in show.php file.
Thanks
MAHABUB
Sorry for late reply. Actually was on vacation on the largest sea beach of the world. Try the below one it should work and let us know.
<?php
foreach ($_POST['cb'] as $k){
echo $bed = $_POST['bed'][$k];
echo "<br/>";
echo $bath = $_POST['bath'][$k];
echo "<br/>";
echo $price = $_POST['price'][$k];
echo "<br/>";
echo $address = $_POST['address'][$k];
echo "<br/>";
}
?>
Thanks
Mahabub
please put only this on ur show.php file.
<?php
print_r($_POST);
?>
and take a look at the post array. Also look at the source of your search.php file I mean the HTML source and take a look that php script generated output correctly.
Also check that that short_open_tag are on in your php.ini
please let us know about your final output.
Thanks
Mahabub