PHP - Using Arrays Or Variables Inside _post
I have the following loop in which I receive Parse error:
$letters = array ("A", "B", "C", "D","E", "F", "G", "H","I", "J", "K", "L", "M", "N", "O", "P","Q", "R", "S", "T","U", "V", "W", "X", "Y", "Z", "Æ", "Ø", "Å"); for ($i = 1; $i < 29; $i++) { if ($_POST['$letters['$i']']){ echo "You have selected the following products:" . "<br>" . $_POST['$letters['$i']]; } } or is it possible to write something like that: for ($i = 1; $i < 300; $i++) { if ($_POST['$i']){ echo "You have selected the following products:" . "<br>" . $_POST['$i']; } } Please, help me. I need to use array and variables inside _POST[]. Similar Tutorialsok so here the deal. i have to write a script that searches a giant database of school and returns the contact info for all schools matching the search term. no big deal it's cake to code something that simple... but wait there more! the search must be executed by a joomla page... we crap. joomla doesn't allow me to run php scripts... so i found a little plugin called jumi that lets me do just that. and it works great minus all the errors joomla spits out because the mysql_connect statement and the mysql_query statment are connecting to a different database than the one joomla is installed onand thus joomla thinks the tables i am searching don't exist so joomla outputs this 4 page long error code in a bright red box a the top of the page. so i tried turning off joomls error reporting.. no bueno. so i figured no problemo i'll just have the script call out to a .php file hosted else where on the server and pass the search terms to that file via $_POST. then have said file execute a quick search of the database and return the results back to the original joomla page from whence the search commenced via a nice tidy $_POST array. one problem how in hades do you pass around php arrays via $_POST? I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! Ok- I have seen it go both ways on this forum and I was wondering which is correct- or more secure. I have a script that receives $_POST variables from a form. Which is better- to change the name of the $_POST variable to do script manipulations or to simple do them with the $_POST['whatever'] $whatever = $_POST['var_from_form']; or simply utilize $_POST['var_from_form'] I know it would be less typing changing it to $whatever, but does it really matter? And yes- register_globals is off. Cheers- I'm building a form using ajax that will allow users to add additional rows to their submissions. The problem however is getting PHP to recognize those variables. So that if the user submits 5 rows, it reads and creates a variable for all 5. I'm pretty sure that doing this will involve some form of while() processing and a total row count, but I'm unsure how to name the variables on the form end and how to receive those into PHP without the script erroring out. Suggestions? I was thinking something along the lines of naming each variable something like "number_title" or as an example "5_title." Then using a while function to pull the data from $_POST[$number.'_title']; or something similar, but I'm unsure if that method would even work. Hello Masters I need to iterate through multiple $_POST[''] variables that have been submitted by a form, from the user, checking to see if they have been set or left unfilled. Having a tough time finding a tutorial online... I am a php noob... So I though that I would assign short variable names to the $_POST variables and put them into and array(with names for keys) , then use a for loop and isset() to echo any variable's key in which its value has not been set to the user. I have found out that I can not use variables as array values.... What is the best way to do this? Thanks to you in advance sirs. foreach ($_POST as $key => $value){ // Handle escape characters, which depends on setting of magic quotes if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){ $value = urlencode(stripslashes($value)); } else { $value = urlencode($value); } $req .= "&$key=$value"; } $reg is not returning anything. Hello everyone, Can someone show me a way how to pass $_post variable from a form to a function? So, input username and input password to a function login($username, $password). I have a php search page that takes form data to create its queries, then reload the original search page. The form is long, so there are numerous $_POST's to convert to variables for use in the query. I've looked at "extract" and a few other code snippets. But it's a form and I don't want any security issues. I'm hoping there's a "foreach" solution, but I'm a beginner. What's a proper way to to simplify this: $s1 = $_POST["s1"]; $s2 = $_POST["s2"]; $s3 = $_POST["s3"]; $s4 = $_POST["s4"]; $s5 = $_POST["s5"]; $s6 = $_POST["s6"]; $s7 = $_POST["s7"]; $s8 = $_POST["s8"]; $s9 = $_POST["s9"]; $s10 = $_POST["s10"]; $s11 = $_POST["s11"]; $s12 = $_POST["s12"]; Hi, Which one is better from performance view (CPU usage and etc)? using too many Variables or a single Associative Array or generally an Array? This one: $ld_linkdump_title = get_option('ld_linkdump_title'); $ld_linkdump_widget_title = get_option('ld_linkdump_widget_title'); $nw_option = get_option('ld_open_nw'); $ld_open_branding = get_option('ld_open_branding'); $ld_stylesheet_option = get_option('ld_stylesheet'); $ld_number_of_links = get_option('ld_number_of_links'); $ld_number_of_links_widget = get_option('ld_number_of_links_widget'); $ld_number_of_rss_links = get_option('ld_number_of_rss_links'); $ld_number_of_links_be = get_option('ld_number_of_links_be'); $ld_repeated_link = get_option('ld_repeated_link'); $ld_linkdump_fd = get_option('ld_linkdump_fd'); $ld_linkdump_rss_desc = get_option('ld_linkdump_rss_desc'); $ld_branding_bg = get_option('ld_branding_bg'); $ld_archive_days = get_option('ld_archive_days'); $ld_archive_pid = get_option('ld_archive_pid'); $ld_show_counter = get_option('ld_show_counter'); $ld_show_description = get_option('ld_show_description'); $ld_show_description_w = get_option('ld_show_description_w'); $ld_send_notification = get_option('ld_send_notification'); $ld_auto_approve = get_option('ld_auto_approve'); $ld_short_url = get_option('ld_short_url'); or this: $options['ld_linkdump_title'] = get_option('ld_linkdump_title'); $options['ld_linkdump_widget_title'] = get_option('ld_linkdump_widget_title'); $options['nw_option'] = get_option('ld_open_nw'); . . . Howdy, I used to have a whole bunch of terms (PreK, Elem, MS, HS etc...) together into a single variable called "levels". They terms were separated by commas. The following code created a pulldown menu that allowed me to "filter" the page for any one particular term. Worked terrific... Code: [Select] <? //remove any old level from query $tmp = array(); foreach ($_GET as $fld => $val) if ($fld != 'levels') $tmp[] = $fld . '=' . $val; $page_name = $_SERVER['SCRIPT_NAME'] . '?' . implode('&',$tmp); ?> <?php echo ' <form name="form4" action="" method="get" class="tight"> <select name="levels" >'; ?> <option <?php if(empty($_GET['levels'])){ echo "selected=\"selected\""; } ?> value="<?php echo "$page_name" ?>">DISPLAY ALL LEVELS</option> <?php if ($_GET[levels] == 'PreK') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=PreK">PreK</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=PreK">PreK</option>'; } if ($_GET[levels] == 'Elem') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=Elem">Elem</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=Elem">Elem</option>'; } if ($_GET[levels] == 'MS') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=MS">MS</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=MS">MS</option>'; } if ($_GET[levels] == 'HS') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=HS">HS</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=HS">HS</option>'; } if ($_GET[levels] == 'College') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=College">College</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=College">College</option>'; } if ($_GET[levels] == 'Prvt') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=Prvt">Prvt</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=Prvt">Prvt</option>'; } if ($_GET[levels] == 'Admin') { echo '<option selected value="'; ?> <?php echo "$page_name" ?> <?php echo '&start=0&levels=Admin">Admin</option>'; } else { echo '<option value="'; ?> <?php echo "$page_name" ?> <?php echo '&levels=Admin">Admin</option>'; } echo ' </select> </form>'; ?> Now, I'm keeping all those terms as separate variables, and combining them in an array. (I think that's right.) Code: [Select] $PreK = $PreK; $Elem = $Elem; $MS = $MS; $HS = $HS; $College = $College; $Admin = $Admin; $Prvt = $Prvt; $levels = array($PreK, $Elem, $MS, $HS, $College, $Admin, $Prvt); foreach ($levels as $key => $v ) if (!$v) unset ($levels[$key]); $levels = implode(', ', $levels); echo $levels; My question, how do I modify my pulldown form (above) to check the WHOLE array for these values? I'm so lost... Thanks. ~Wayne Hello.
Server returns this json
{"status":1,"job_status":"READY","iterations":["Contig2","Contig75"],"accessions":["NP_001061702","NP_001068174"]}And i would like to print out in a textarea (using the above jQuery command) $('#info').val()something like this: Contig2 : NP_001061702 Contig75 : NP_001068174 Any idea on how to achieve this? Thanks! Hi I have a page that the user selects a category and I have the page sending a variable to a page. However, I want the user to be able to select multiple options and therefore need the page doing the query to build an array and change the search results accordingly! Please can someonehelp me with what I have as I am jsut NOT getting my head around this! Here is the 'form' info Code: [Select] <input name="jqdemo" value="6" type="checkbox" id="party"/> <label for="party"></label> <a class="checkbox-select" href="# id="6" onclick="showUser(this.id)""><img src="Assets/images/blank.png" alt="" width="120" height="180" /></a> <a class="checkbox-deselect" href="#" id="0" onclick="showUser(this.id)"><img src="Assets/images/blank.png" alt="" width="120" height="180" /></a> Then this is the page that is doing the query Code: [Select] <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db_pyf", $con); //$sql="SELECT * FROM tbl_product WHERE cat_id IN '".$q."'"; $sql="SELECT * FROM tbl_product WHERE cat_id IN (3,1,2)"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['prod_name'] . "</td>"; echo "<td>" . $row['prod_desc1'] . "</td>"; echo "<td>" . $row['member_id'] . "</td>"; echo "<td>" . $row['cat_id'] . "</td>"; echo "<td>" . $row['cat_id'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> But, as I have said, I want the user to be able to select multiple options and want the search to adjust accordingly! Hello, I am a PHP biginner. I would be grateful if you could help with the issue below. I have created three input dynamic variables in a separate php file using a For lopp.
<?php
The form works well. In another PHP I would like to use the three arrays workday[], startTime[] and endTime to calculate the duration between startTime and endTime for each working day. I can access the different arrays using Foreach like below but I don't see how to combine thos arrays in order to be able to calculate the duration between two data that are in diffrent arrays. Is it possible to make calculations using variables stored in different arrays ? If so could you help me understand how ?
<?php
if (isset($_POST['workday'])) { echo $workday . "<br>;
}
echo $startTime. "<br>;
if (isset($_POST['endTime'])) {
echo $endTime. "<br>;
Hello, I've created a function whereby I want to return the school_id associated with a particular user. Each user will be associated with exactly one school. My query below works, but the thing that it returns is an array; I need to make use of it as a variable (say $instructor_school_id). Any idea how to make the conversion from a single element array into a non-array variable? Thank you.... function getInstructorSchool($read, $user_id) { $sql = "SELECT school_id FROM users WHERE user_id = $user_id"; return $read->fetchRow($sql); } As the title says, how can i check if multiple arrays exist inside an array or not? Any help is greatly appreciated!
Okay I have a function that stores data into an Array. The function takes about 7 seconds to run and through a number of different loops it creates one final array with about 15,000 keys. I want to recall this data a number of times in different functions, however how can I have this data easily accessible without running the function each time. EX: Code: [Select] function theFunction() { for($x=0;$x,=15000;$x++) { //Run the loop and store data. $string[$x] = //output from other loops and calculations } return $string } //Then later on if I want to recall the data the only way I know how is to do the follow: $newstring = theFunction() The only problem I have with this is that it has to re-run the function every time in order to get to the data it spits out. How can I store this data into another array outside of the function without having to re-run it? I hope this makes sense. Thanks. So say I have a file with the contents.. Code: [Select] ; this is a comment var2 : variable ; another comment var3 : file.txt how would I be able to do <?php echo $var2; ?> That would echo "variable" and <?php $file = file($var3); ?> So its reading the file to get the variable from the text file and if the line starts with ";" its disregarded.. Thanks Hello. Trying to learn MVC better by creating my own little framework to understand how it works. Things were going OK til now. I have a base class: <?php /** * Base class most classes will extend from. * Simply put, this class just has methods that * most, if not all, classes will need. */ class Application { public function includer($path) { if (is_readable($path) == true) { include_once($path); } else { die("404 not found =["); } } } ?> So the above method 'includer' just sees if a file exists/is readable and if so, include it. Here's where I am using said method. class Index extends Application { function __construct($method = 'view') { // load the index model include(ROOT . '/app/models/model.index.php'); // Invoke requested method. $this->$method(); } public function view() { $name = 'Smith'; $this->includer(ROOT . '/app/views/view.index.php'); # Problem here, i think } } view.index.php just contains <?php echo $name; ?> All that is called from the index page, with this line of code Application::includer($controller_path); Now, in line $this->includer(ROOT . '/app/views/view.index.php'); # Problem here, i think If i get rid of $this->includer, the script will work and say 'Smith'. If i have $this->includer(...) or parent::includer(...), it doesn't work. why? I have the array defined and print_r (outside the function is show what I need...) However, I need to somewhay pass those three arguments - name, price, shipping (keys & values) into the "all_products" function and assign (inside the function) each element to a variable {name, price, shipping}. Code: [Select] function all_products($key,$value) { # This is where I'm lost - I know the values are passed to the function but how to I assign them to below variable? echo "$name"; echo "$price"; echo "$shipping"; // Products array defined to send values into "all_products" function & loop to display item name & individual pricing (using WHILE LOOP) $products = array('Product1' => array ( 'name' => 'item one', 'price' => '30.00', 'shipping' => '0.00'), 'Product2' => array ( 'item two', 'price' => '19.50', 'shipping' => '0.10'), 'Product3' => array ( 'item three', 'price' => '21.99', 'shipping' => '0.10') ); while (list($keys, $values) = each($products)) { while (list($k, $v) = each($values)) echo "Checking Looped Data Outside Function: <strong>$k: </strong> $v<br/>"; # echo "<pre>"; # print_r($value); # echo "</pre>"; # Trying to send array elements into "all_products" function & echo (name, price, shipping) keys and values inside that function all_products($k,$v); } |