Page 1 of 1

Am I thinking the right way? Using radio button to search...

Posted: Mon Jan 05, 2004 12:19 pm
by crazytopu
Hi,

this is all to do with search. People search book by call_no, author, title...etc.


I am running into a problem. when i tried to write the code for this search functions..here i explain it in details...


i let the users to select one of the 5 radio buttons namely call_no, author, isbn, category,title.

I place 5 text box, one against each radio button to take the value that the user enters after he made the selection.

Then he hits the search button...my code first checks which radio button is selected, and then takes the value from the corresponding text box and send it to my php file that will process back the result and display it to the users.

so, depending on the selection my sql statement will change:

when the selected radio button is call_no it would be:

Code: Select all

<?php 
$sql = "Select * FROM book WHERE call_no=('{$_POST['call_no']}')"; 

?>



when the selected radio button is author it would be:

Code: Select all

<?php 
$sql = "Select * FROM book WHERE call_no=('{$_POST['author']}')"; 

?>




and so on........................


Now, i m not sure how to let my code to check which radio button is selected.

the following code i have copied from my html page:

Code: Select all

<?php 

<td width="15%"> 
      <p align="right"><input type="radio" value="V1" checked name="R1"></p> 
    </td> 
    <td width="6%">Call No</td> 
    <td width="51%"><input type="text" name="T1" size="20"></td> 
    <td width="57%"><a href="http://localhost/insertbook.php"><font color="#0000FF">Insert 
      Entry</font></a></td> 
  </tr> 
  <tr> 
    <td width="15%"> 


    <p align="right"><input type="radio" name="R1" value="V2"> 


    </td> 
    <td width="15%"> 

Author 

?>




Here, the first radio button is selected by default, so it has an extra property PHP:

<?php checked name="R1" ?>




I wonder if i should let the code to check "checked" value...

Am i thinking the right way?

If you know more specific and more effective way please let me know.

Code: Select all

// Added PHP tags for eyecandy. --JAM

Posted: Mon Jan 05, 2004 1:42 pm
by xisle
For your SELECT to work, the radio buttons should all have the same name but different values.. this insures that only one is selected.
Like this...

Code: Select all

<input type="radio" name="author" value="nobel-2003">J.M. Coetzee
<input type="radio" name="author" value="nobel-2002">Imre Kertész
<input type="radio" name="author" value="nobel-2001">V.S. Naipaul
<input type="radio" name="author" value="nobel-2000">Gao Xingjian

Posted: Tue Jan 06, 2004 3:08 am
by crazytopu
I want to write a php code which will do something like this:



if(selected==call_no) //here call_no is the name of my radio button
{

require(http://localhost/call_no_search.php);//this php file displays the search results

}

else if(selected==author)
{

require(http://localhost/author_search.php);

}

else if(selected==isbn)
{

require(http://localhost/isbn_search.php);

}



Did anybody do it before? or similar type of stuff?

Posted: Tue Jan 06, 2004 3:09 am
by crazytopu
I want to write a php code which will do something like this:

Code: Select all

<?php

if(selected==call_no) //here call_no is the name of my radio button
{

require(http://localhost/call_no_search.php);//this php file displays the search results

}

else if(selected==author)
{

require(http://localhost/author_search.php);

}

else if(selected==isbn)
{

require(http://localhost/isbn_search.php);

}

?>
Did anybody do it before? or similar type of stuff?

Posted: Tue Jan 06, 2004 3:42 am
by mwong
switch statement could be used

Code: Select all

switch($selected) &#123;

case 'call_no':
//search call_no
break;

case 'author':
//search author
break;

case 'isbn':
//search isbn
break;

&#125;

Posted: Tue Jan 06, 2004 6:24 am
by crazytopu
thanks for your help..but i am not worried about the if else or switch statement.

I am yet to figure out how to check which radio button is selected. Is there any function in php?

i have just selected my first radio button so its property got changed to

Code: Select all

<td width="9%"><input type="radio" value="V1" name="R1" checked></td>
from

Code: Select all

<td width="9%"><input type="radio" value="V1" name="R1"></td>

As u can see a new word has been added " checked".

Posted: Tue Jan 06, 2004 8:59 am
by crazytopu
I am almost close to the solution just need a bit more help:

Code: Select all

<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 



if($R1=='V1')&#123;   // R1 my radio button and V1 is its value


$sql = "Select * FROM book WHERE call_no=('&#123;$_POST&#1111;'call_no']&#125;')";&#125;

if($R1=='V2')&#123;

$sql = "Select * FROM book WHERE author1=('&#123;$_POST&#1111;'author']&#125;')";
&#125;

if($R1=='V3')&#123;

$sql = "Select * FROM book WHERE title=('&#123;$_POST&#1111;'title']&#125;')";
&#125;

if($R1=='V4')&#123;

$sql = "Select * FROM book WHERE category=('&#123;$_POST&#1111;'category']&#125;')";
&#125;

if($R1=='V5')&#123;

$sql = "Select * FROM book WHERE isbn=('&#123;$_POST&#1111;'isbn']&#125;')";
&#125;


$result = mysql_query($sql); 

?> 


<table border=2> 
  <tr> 
    <td>call_no</td> 
    <td>title</td> 
    <td>author1</td> 
    <td>author2</td> 
    <td>publisher</td> 
    <td>isbn</td> 
    <td>price</td> 
    <td>no_of_page</td> 
    <td>edition</td> 
    <td>category</td> 
  </tr> 


<?php 


while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125; 
?> 

</table>


Just need a bit help...with this code

Code: Select all

if($R1=='V1')
what's wrong with this if statement? should i use isset function?

Posted: Tue Jan 06, 2004 9:17 am
by JAM
Continued in another thread.
viewtopic.php?t=16343