Jump to content

How to pass a variable from a form to limit data displayed


bkassar

Recommended Posts

Hello. I am quite new to PHP and MySQL. So please forgive me. I would like to do the following.

1.Allow a user to identifity a city they are interested in

and

2Allow a user to read information others have submitted about that city.

 

Code for page 1 is as follows:

<form action="results.php" method="post" class="style2">

  <div align="center">

    <p>Enter a City<br />

      <br />

      <input type="text" name="citysearch" />

    <input type="submit" value="Search" />

 

Code for page 2 is as follows (but i don't want it to be hardcoded "ixtapa" as i have below.. i want to limit the data displayed by the the variable the user generates in page 1. Please help :-)

 

<?php

$query="SELECT * FROM Questions WHERE City='ixtapa'";

$result=mysql_query($query);

 

$num=mysql_numrows($result);

 

mysql_close();

 

?>

 

<table border="0" cellspacing="2" cellpadding="2">

<tr>

<th><font face="Arial, Helvetica, sans-serif">Name</font></th>

<th><font face="Arial, Helvetica, sans-serif">Where</font></th>

<th><font face="Arial, Helvetica, sans-serif">"Home"</font></th>

<th><font face="Arial, Helvetica, sans-serif">Other Places</font></th>

<th><font face="Arial, Helvetica, sans-serif">I Wish</font></th>

</tr>

 

<?php

$i=0;

while ($i < $num) {

 

$f1=mysql_result($result,$i,"Name");

$f2=mysql_result($result,$i,"City");

$f3=mysql_result($result,$i,"Loc");

$f4=mysql_result($result,$i,"Other");

$f5=mysql_result($result,$i,"Before");

?>

 

<tr>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>

</tr>

 

<?php

$i++;

}

?>

 

 

 

Link to comment
Share on other sites

Use the mysql LIMIT feature.  It accepts ONE or TWO arguments.

 

SELECT * FROM field_name WHERE name='Fred' LIMIT 1;

 

or

 

SELECT * FROM field_name WHERE name='Fred' LIMIT 15, 30;

 

When you use the second argument, the first argument becomes the result number you start at, and the second option is the total number of results, if there are that many results.  This is also supposing that the second example query I gave you had over 45 rows in it.  So basically it starts at result #15 and shows the next 30 records from there.

Link to comment
Share on other sites

Thank you BioBob.

I didn't describe my question quite clearly enough. I'm sure what I am asking is quite fundamental.

To use your example... how would I make the name variable dynamic.

Instead of WHERE name='Fred' I'd like it to be WHERE name= something the user typed on the prior page

Thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.