PHP - Referencing Arrays
Here's where I am thus far:
Similar TutorialsHi Is it possible in PHP to reference a dll, I have seen a few things that suggest this is possible but nothing that shows a simple example. thanks I haven't posted here before but I am completely stuck. I've checked everywhere and cannot find answer to this question. I have an array and if I were to do this: echo $partNames[$jjk]; it would return another part of an array as such: $decoded[0]["Parts"][3]["BodyPart"] (if I retrieve type, this is a string). Now, I need to return the actual value from particular array reference and I cannot for the life of me do it and its destroying my will to live. i've tried combinations of: echo $partNames[$jjk]; echo $$partNames[$jjk]; echo ${$partNames[$jjk]}; echo ${$partNames}[$jjk]; $sssttt = substr($partNames[$jjk], 1); echo $sssttt.' - '.${$sssttt}; Yet nothing seems to work. Solution is to rewrite whole script but this is now pretty complicated parser and I do not want to rewrite sections as deadlines are looming. Any help would be appreciated. (I'm new but i'll try to help others now in anticipation of help). Cheers Hello, Does anyone know how to get a substrig by referencing other substrings around it? Ie, my string is the following: These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul> I need to get the ID of each list item, i know they will always follow "<li id=". I also need to get the text of each list item, i know they will always be before "</li>" Thanks! Hi guys, As a uni student i have to reference alot and my uni has its own prefered system. I would like to make referencing as easy as possible. http://library.scotch.vic.edu.au/research/biblios/ScotchBib/index.htm has a neat system that makes referencing quite easy. My question is how would i start the coding. When the form is sent all it does is string together the inputed text and numbers. What would this code look like and how could i display it in a 'Select All' Box? This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=322595.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=325339.0 Hi I am in the process of converting to Object Oriented from Procedural. To cater for this I have built an admin_login function, contained within a class: 'siteFunctions'. However, I am having trouble pointing the admin form to the function correctly. Every time I click 'submit', the form does not process anything. It doesn't even 'think' about it i.e. show the egg timer.... I have built this script heaps of times using the procedural method, so I guess I am somehow doing something wrong with respect to referencing the action attribute of the form (due to my new approach). I am very new to OO so please go easy on me: I know the script isn't particularly advanced. I just want to get used to putting functions into classes, and then calling the code, before I move onto more advanced stuff. I have placed all of the files within the same folder in order to rule out driectory path issues. Here are the three scripts that I think are relevant (login, functionsClass, and the mysql connection script): Login $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); new siteFunctions(); echo '<div class="admin_main_body">'; <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]);?>" method='post'> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> echo '<div>'; include("includes/admin_footer.php"); sitefunctions.php //$page = "admin_index.php"; class siteFunctions { var $message; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($_POST['username'])) { $u = FALSE; $message .= '<p> Please enter your username </p>'; } else { $u = escape_data($_POST['username']); } if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $p = escape_data($_POST['password']); } if ($u && $p) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$u') AND password=('$p')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); //echo '$_SESSION["admin_id"]'; } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Any help would be appreciated. Cheers Will 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! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have two arrays, both with the same key values. I'd like to combine them. So for instance... Code: [Select] <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? The script for creating a new file name for the image:
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) $file_extension = end($ext); //store extensions in the variable $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1]; $target_path = $target_path . $new_image_name;//set the target path with a new name of imageThe script creates a new file like: f6c9b8d9db05366c3504210cded9ddb2.jpgand moves the file to the "uploads" folder. And then the script also creates a thumbnail with the same file name and moves the file to the "thumbs" folder. The issue I am having is that the same ID code could happen again for a different image in the database, thus I would be calling a different original sized image than the thumbnail image. My question is: How to avoid this issue of the same ID code has happened again for a different file. What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image? With the script I have the thumbnail image would be coming from the "thumbs" folder and the anchor tag would get referenced to the "uploads" folder to get the original sized image. Edited by glassfish, 12 October 2014 - 05:51 AM. Let's say I have an array like this: Man1(0), Man1(1), Man1(2), etc. Man2(0), Man2(1), Man2(2), etc. Man3(0), Man3(1), Man3(2), etc. ... and I want to loop through the array. I know I can do: Code: [Select] $i=0; while $i<=99 {//...do something with Man1[$i];} i++; but can I also do a loop with each Man? Like: Code: [Select] $i=0;$a=1; while $i<=99 {//...do something with Man$a[$i];} i++;a++; I've written all the code for each Man separately and I'm thinking there's got to be a way to shorten the code with another loop, but I don't know how. Hope that makes sense. Thanks. Code: [Select] $data=array('user1'=>'pass1','user2'=>'pass2'); now i would like that echo prints out the user1 word. Code: [Select] echo $data[?what here?]; I've spent many hours trying to solve this with no success. So, here's my challenge. Sample Data from DB: Month Year Measure 5 2010 164 6 2010 31 7 2010 20 8 2010 10 9 2010 10 10 2010 10 12 2010 10 1 2011 10 I need to display this data in a chart, but notice that I don't have data for month 11 and the chart must show all months with data and the month that I don't have data for (11 in this case) will show an interrupted line. I need to put this into an array to display month/year as label and measure as chart value. So, the above data needs to become: Month Year Measure 5 2010 164 6 2010 31 7 2010 20 8 2010 10 9 2010 10 10 2010 10 11 2010 -- 12 2010 10 1 2011 10 The issue here isn't the chart, but how to add the missing month (11) to the array and keep the measures corresponding with their months. Thank you. Hi i'am try to insert multiple data on a single submit, but i'm a total noob to Arrays, can any one help me out here... one of the members send me this out, but still i can figure it out how to insert the fields onto my database. it inserts but doesnt get the valuee.. Please help! if(isset($_POST['save'])) { unset($_POST['save']); $i = count($_POST['id']); $x = 0; for($x=0; $x <$i;$x++){ foreach($_POST as $data =>$d){ echo $data.'-'.$d[$x].'<br/>'; $insert = mysql_query("insert into tbl_grades"); //sql statement here (example): insert into table name (value of $data) values( value of$d[$x]) -- can anyone help me with this , how can i query to get the value of this, //HOW DO I MAKE MY SQL QUERY TO SAVE THIS OUT, I KNOW THIS IS MY PROBLEM IN THE QUERY.... here's my HTML <input name="id[]" type="hidden" value="<?php echo $row['id'];?>" /> 1st<input name="fg[]" type="text" id="fg[]" size="1" value="<?php $g_sub['sub_id'];?>"/> 2nd<input name="sg[]" type="text" id="sg[]" size="1" value="<?php $g_sub['sub_id'];?>"/> 3rd<input name="tg[]" type="text" id="tg[]" size="1" value="<?php $g_sub['sub_id'];?>"/> 4th <input name="forg[]" type="text" id="forg[]" size="1" value="<?php $g_sub['sub_id'];?>"/> ---- sub_id stands for subject id Hi, I'm having some trouble trying to generate my random string for a name. This is my code: Code: [Select] <?PHP function rand_string( $length ) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen( $chars ); for( $i = 0; $i < $length; $i++ ) { $str .= $chars[ rand( 0, $size - 1 ) ]; } return $str; } $salt_string = rand_string( 5 ); $test = "1234"; echo "salt_string = $salt_string"; echo "<br>"; echo "test = 1234"; echo "<br>"; $array = array('$test', '$salt_string'); echo "<br>"; echo "array = $array"; echo "<br>"; $imploded = implode($array); echo "<br>"; echo "imploded = $imploded"; ?> And it returns: salt_string = 8pbAe test = 1234 array = Array imploded = $test$salt_string I don't understand why my imploded variable is not 12348pbAe I can't work out the quotes around this... $bookingID['$row['id']'] = $row['id']; echo $bookingID['$row['id'];']; Parse error: parse error, expecting `']'' What would fix this? I'm not sure if this can be done, but I would like to get the sum for each $.row[''] result ex: if $row['robotics'] had values of 100,150,300 I would like to be able to show a total of 550 Code: [Select] <?php echo "<Table border=1>"; echo "<tr><td></td><td>Robotics</td><td>Chiral Stuctors</td><td>Enriched Uranium</td><td>Mechanical Parts</td><td>Coolant</td><td>Consumer Electronics</td><td>Precious Metals</td><td>Reactive Metals</td><td>Oxygen</td><td>Toxic Metals</td></tr>"; $result = mysql_query("select * from `pi`"); while ($row = mysql_fetch_array($result)) { echo "<tr><td>".$row['pilot']."</td>"; echo "<td>".$row['robotics']."</td>"; echo "<td>".$row['chiralstuctors']."</td>"; echo "<td>".$row['enricheduranium']."</td>"; echo "<td>".$row['mechanicalparts']."</td>"; echo "<td>".$row['collant']."</td>"; echo "<td>".$row['consumerelectronics']."</td>"; echo "<td>".$row['preciousmetals']."</td>"; echo "<td>".$row['reactivemetals']."</td>"; echo "<td>".$row['oxygen']."</td>"; echo "<td>".$row['toxicmetals']."</td></tr>"; } echo "</table>"; ?> I have some similar arrays. How I can combine them to shuffle the new array? Hello, I'm trying to extract information about a ticket through osTicket SOAP API, but I can't seem to figure out how! This is the return from the script (var_dump): array(1) { [0]=> object(stdClass)#2 (2) { ["question"]=> object(stdClass)#3 (4) { ["id"]=> int(25) ["created"]=> string(25) "2011-01-05T01:19:43+01:00" ["name"]=> string(4) "test" ["message"]=> string(4) "qasd" } ["answers"]=> array(1) { [0]=> object(stdClass)#4 (4) { ["id"]=> int(25) ["created"]=> string(25) "2011-01-05T01:25:06+01:00" ["name"]=> string(17) "Andreas Haakonsen" ["message"]=> string(3) "ler" } } } } A ticket can have several answers. I just can't seem to figure out how to extract/use this data and present it on my webpage. Too many arrays for me to understand I guess :p Is there anyone who can help me with some small code to help me figure this one out? Thanks! |