Current location: Hot Scripts Forums » Programming Languages » PHP » Accessing PHP array in HTML to make radio buttons


Accessing PHP array in HTML to make radio buttons

Reply
  #1 (permalink)  
Old 10-13-05, 11:39 AM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Accessing PHP array in HTML to make radio buttons

hello..

I'm a newbie to php and mysql so here i am.. I'm strugglin with something..this is the story..

I have an array in PHP with strings in it.. now i need to make radio buttons with the values being the ones in this array but i dono how to access the contents of the PHP array in HTML so that I can create a radio button for each of the PHP array's members..and then get the name of the radio button that was selected by the user..

Any help will be appreciated.. Thank you..
Reply With Quote
  #2 (permalink)  
Old 10-13-05, 11:57 AM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Firstly you'll need to make sure the page has the correct file extension, the page name should be something like "myPage.php" (not "myPage.html").
Secondly, and most importantly, comes the actual PHP for printing each array option. You could do something like the following:
PHP Code:

echo("<form method=\"POST\" action=\"answer.php\">");

// This prints the form tag, directing the page to "answer.php" once submited
    
echo("<fieldset><legend>Array Options</legend>");
    
// This prints the fieldset for the radio buttons, to indicate that only 1 radio button can be selected within this area

    
for ($i=1$i<=count($array); $i++) {
    
// This loops from 1 to the number of entries in the array, aka the count() part
        
echo("<input type=\"radio\" name=\"radioOption\" value=\"" $i "\" id=\"option" $i "\"><label for=\"option" $i "\">" $array[$i] . "</label>")
        
// This basically sets up a radio button called radioOption, with the value of $i and the id option$i (i.e. option6)
        // The label is related to the option id (i.e. option6) and displays $array[$i], basically the array option from the PHP array.
    
}
    echo(
"<input type=\"submit\" value=\"Submit Option\"></fieldset>");
echo(
"</form>"); 
You'll then need to make an answer.php page to view the option that was choose, you can do this by using the $_POST function to get the value of the string "radioOption"
PHP Code:

echo($_POST['radioOption']); 

Hope that helps out a bit.

Last edited by Richard; 10-13-05 at 12:00 PM.
Reply With Quote
  #3 (permalink)  
Old 10-13-05, 12:09 PM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you for ur reply.. I'm a bit confused.. So this first part with the radio option part should go in the php file were i have my array with the strings in it ?

I've been searching the php.net website and found this
PHP Code:



<?php
 $line_class
=array();
 
$line_item=array();
 
   for (
$i=$rows_number;$i>0;$i--) 
     {
      
$line_class[$i]=$_POST['line_class'.$i.''];
      
$line_item[$i]=$_POST['line_item'.$i.''];
     }
?>
its at the webiste: http://www.php.net/FAQ.html

I have no idea what they are doing here..
Reply With Quote
  #4 (permalink)  
Old 10-13-05, 12:20 PM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I'm trying to use the method you gave me this line is giving an error:


PHP Code:

 echo("<input type="submit" value="Submit Option"</fieldset>"); 

sorry i'm a complete newbie so myabe i'm not catching on to the syntax error.. thanks for ur help..
Reply With Quote
  #5 (permalink)  
Old 10-13-05, 01:45 PM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Newbie2005
Thank you for ur reply.. I'm a bit confused.. So this first part with the radio option part should go in the php file were i have my array with the strings in it ?
That code should go wherever you want to display the radio buttons in your main file.
Quote:
Originally Posted by Newbie2005
I have no idea what they are doing here..
hehe, nps, we all have to start somewhere.
Quote:
Originally Posted by Newbie2005
sorry i'm a complete newbie so myabe i'm not catching on to the syntax error.. thanks for ur help.
That isn't your fault at all. When i first posted the sample, i didn't realise that i had made a mistake on that line, try using the edited version in the post now; it should work.
Reply With Quote
  #6 (permalink)  
Old 10-14-05, 02:36 AM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
hi..

Thanx it works now.. it was giving me a notice about array offset and it wasnt displaying the first member of the array so i changed this line ( puuting it here in case someone uses this code later):

PHP Code:



for ($i=0$i<sizeof($table_names); $i++) 
Ok now that this is working my next hurdle is to get the value of the radio option from answer.php and use that index in the array to obtain that array element and execute a query in my main page which uses that array element..

so like if radio option value =1 then i use that array element as the destination of a MYSQL query i'm executing..

How can i go about this... Thanks so much for ur help !!!
Reply With Quote
  #7 (permalink)  
Old 10-14-05, 04:57 AM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Newbie2005
so like if radio option value =1 then i use that array element as the destination of a MYSQL query i'm executing..

How can i go about this... Thanks so much for ur help !!!
You'll need to query the MySQL database using a few function...
PHP Code:

$dbh mysql_connect ("localhost", <username-here>, <password-here>) or die ('I cannot connect to the database because: ' mysql_error());

// This function connects to the database with <username-here> using <password-here>
mysql_select_db ("dbName"); 
// This function selects which database you want to choose

$result mysql_query("SELECT * FROM dbName WHERE id='" $_POST['radioOption'] . "'");
// This is the query that selects which data you want and from where

while($info mysql_fetch_array($result)) {
// This loops through each record that has the 'right' id
    
echo($info['id']);
    
// Prints the id of the current info. Change ['id'] to the field name you want to print
}

mysql_close($dbh)
// Closes the database connection 
Bit confusing at first, but you'll soon get used to using MySQL.
Reply With Quote
  #8 (permalink)  
Old 10-14-05, 09:26 AM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Ok i'm a lil confused.. 'radiooption' only gets a value once the submit button has been pressed right but.. after the submit button has been pressed the page goes to answer.php.... but i need to have the mysql query executed using some variables i saved in the main page...

This is my code in the main page, where i'm loading a file, saving it in a destination and then getting the filename i need for my "load data infile query" the radio button selection is supposed to give me the name of the table in the databse where i load the data...

PHP Code:



else                                               
    {
        
$destination 'C:\Program Files\MySQL\MySQL Server 4.1\data\tags'."\\".$_FILES['filename']['name'];
        
$temp_file $_FILES['filename']['tmp_name'];
        
//echo $temp_file; 
        
move_uploaded_file($temp_file,$destination);
        echo 
"<p><b>The file has successfully uploaded:</b>
            
{$_FILES['filename']['name']} 
            (
{$_FILES['filename']['size']})</p>"
        
        
$db mysql_connect($sqlserver,$sqluser,$sqlpass);
        
mysql_select_db($database,$db);

        
$test ="Load data infile '"
        
$test .= $_FILES['filename']['name']; 
        
//echo $test;
        
$part2 "' INTO TABLE ";  //registered_tags 
        
$test .= $part2
        echo 
"$test <br><br>";
        
        
//$sql= "LOAD DATA INFILE 'expected.txt' INTO TABLE registered_tags LINES TERMINATED BY '\n' (tag_reg)";

            
$sql "SHOW TABLES FROM $database";
            
$resultmysql_query($sql);            //mysql_list_tables($database); //= 

            
if (!$result
            {
               echo 
"DB Error, could not list tables\n";
               echo 
'MySQL Error: ' mysql_error();
               exit;
            }

            while (
$row mysql_fetch_row($result)) 
            {
                
//echo "Table: {$row[0]}\n";
                
if ( strncmp ($row[0], 'reg',3) ==0) {
                echo 
"Table: {$row[0]}\n";
                
$table_names[]= $row[0];}
            }
            
            for (
$i=0;$i<sizeof($table_names);$i++)
            {
                echo 
"$table_names[$i]<br>";
            }
            
            
//print_r($table_names);
            
            
echo("<form method=\"POST\" action=\"answer.php\">"); 
            
// This prints the form tag, directing the page to "answer.php" once submited 
            
            
echo("<fieldset><legend>Array Options</legend>"); 
            
// This prints the fieldset for the radio buttons, to indicate that only 1 radio button can be selected within this area 

            
for ($i=0$i<sizeof($table_names); $i++) 
            { 
            
// This loops from 1 to the number of entries in the array, aka the count() part 
            
echo("<input type=\"radio\" name=\"radioOption\" value=\"" $i "\" id=\"option" $i "\"><label for=\"option" $i "\">" $table_names[$i] . "</label>");
            
// This basically sets up a radio button called radioOption, with the value of $i and the id option$i (i.e. option6) 
// The label is related to the option id (i.e. option6) and displays $table_names[$i], basically the array option from the PHP array. 
            

            echo(
"<br> <Br> <input type=\"submit\" value=\"Submit Option\"></fieldset>"); 
            echo(
"</form>"); 
/******************************
can only get radioption once submitted.. that takes me to answer.php but need it here to build my query with the right table name which is the element in the array indexed by the value of radiooption 
    
    *****************/
    
            
$test.= $_POST['radioOption'];
            
$part3 " LINES TERMINATED BY '\n' (tag_reg)";  //this is the last part of the query 
            
$test.= $part3
            
            
//$sql= "LOAD DATA INFILE 'expected.txt' INTO TABLE registered_tags LINES TERMINATED BY '\n' (tag_reg)";
            
$result mysql_query($test);
            
            if (
$result == 0)
            {
               echo 
"<b>Error ".mysql_errno().": "mysql_error()."</b>";
            }
            
            
mysql_free_result($result);
        
        
//}
   
//end of inner else 
   
  
// end of outer else 
Reply With Quote
  #9 (permalink)  
Old 10-14-05, 02:51 PM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Newbie2005
Ok i'm a lil confused.. 'radiooption' only gets a value once the submit button has been pressed right but.. after the submit button has been pressed the page goes to answer.php.... but i need to have the mysql query executed using some variables i saved in the main page...

This is my code in the main page, where i'm loading a file, saving it in a destination and then getting the filename i need for my "load data infile query" the radio button selection is supposed to give me the name of the table in the databse where i load the data...
Easily solved. In the <form> tag near the radio buttons, change the action preference to your main page, instead of answer.php.
Reply With Quote
  #10 (permalink)  
Old 10-15-05, 09:06 AM
Newbie2005 Newbie2005 is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
But if goes back to my main page after the submit button is hit then will it remember previous variables (e.g. $test ) that i had stored in the main page as i need them and the value that radioOption index in the array to execute my query..

I don understand completely how I must go about getting that radio button
value on the uploadfile.php page and using it there itself to execute the
query...

Ok lemme start from the beginning and explain it all properly (I hope)

1. User goes to Form_upload.html (attached) page were a text file is uploaded and once the "Upload File" button is hit the page is redirected to
Uploadfile.php.

2. In Uploadfile.php first it checks whether the "Upload File" button
was hit, if it was then it starts of with the else part..

3. In the else part.. it moves the uploaded file to a local destination and
then checks which tables in the database start with 'reg' stores them in
$table_names and then displays radio buttons for them on Uploadfile.php page...

4. A radio button is selected and the Submit button hit..

5. This is where i'm not sure what approach to take.. then should it go to
Uploadfile.php again and execute the query and go to another page
that just informs the user that "File was sucessfully loaded into
database"

Ok i'm not sure how PHP works with that $ BUT after the hitting the
"submit" button when it comes back again to the Uploadfile.php page ..will it remember the variables that were created previously like the filename that was stored and the partly formed query thats stored in string variable $test... coz I need those variables for executing the database query..

OR

would it be a easier/better idea to get the name of the file loaded and the
name of table obtained from the radioOption value into another page and
execute the query there ?? (how do i get these variables across to the new
page)..

Ok I think I've written a long enough story.. phew.. n by now u must be tired
of reading this..

Form_uplaod.html, Uploadfile.php and answer.php are all attached..

Thanks for the help !!!!
Attached Files
File Type: zip files.zip (2.7 KB, 152 views)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Please help insert html into php idforforums PHP 5 04-06-05 11:29 PM
How to Make my Php output write static Html files cebuy PHP 1 02-04-05 05:52 AM
PHP w/ multiple radio buttons...Help... doublee313 PHP 7 09-15-04 12:49 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 06:17 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.