PHP - Extracting Data From A Multidimensional Array And Populating Db With The Values
Hi all,
I hope someone can help me out here as I think I am close but am not sure where to go from here. The problem is: I have a cms where a user can see a list of photos in the gallery. I provide them the ability to select what photos they want to appear on a particular webpage. The problem I have is that the user wants to be able to put the photos in whatever order they choose. So, I have a form (see attached), the html code is: <table width="566px" cellspacing="0" border="0"> <tr> <td width="33%"> <div class="serviceCell"> <input type='checkbox' checked='checked' name='photos[][0]' value='119' /> Bathroom Sin... <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value="0"/> </div> </td> <td width="33%"> <div class="serviceCell"> <input type='checkbox' checked='checked' name='photos[][0]' value='113' /> Courtyard 1 <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value="0"/> </div> </td> <td width="33%"> <div class="serviceCell"> <input type='checkbox' name='photos[][0]' value='114' /> Courtyard 2 <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value=""/> </div> </td> </tr>... Now, lets assume that the user ticked the box next to 'Courtyard 2' and entered an integer '1' in the input box to signify that this image should be displayed 1st on the chosen webpage. These 2 values go into the multidimensional array (photoid, ranking) and are submitted in the form to php in the photos[][] array. But here is where I have the problem. I am trying to extract the values from the array and then do an insert to a table in mysql. My syntax must be wrong somewhere so I am asking anyone to please offer any advice: if (isset($_REQUEST['submitted'])){ $photos = $_REQUEST['photos']; ... if(!empty($photos)){ foreach($photos as $photoID){ $sql = "INSERT IGNORE INTO menuItemPhotos SET photoid='$photoID[0]', menuItemid='$id', ranking='$photoID[1]'"; $ok = @mysql_query($sql); // execute the query } // End of foreach($photos as $photoID){ } // End of if(!empty($photos)){ Thanks Conor [attachment deleted by admin] Similar TutorialsHey all, I wrote a crawler and have already written the string to extract links. i get a multidimensional array. this is #13 for example Array[0][13] => <a class="menuitem" href="/home-news.html" title="Home / News">Home / News</a> Array[1][13] => /home-news.html ok so far so good. Now I want to extract the name Home / News and write that into a database and the link as well. can i use the preg_match_all() to search an array? or how can i extract it? Thanks so much! monkover I have a multidimensional array for a calendar application as shown below. The first level is an associative array for calendar dates, and the second is a numeric array for multiple events on each day. The third level holds the events. I know that I can extract a single event using the array indices, such as: echo $events['2010-11-24'][0]['event_date']; If I know the number of events for the date, I can extract them one-by-one by incrementing the array index. However, I never know how many events will be stored for any one day. I also know that I can extract all the data in the entire array using a foreach loop. But how can I extract only the data in a subarray? Array ( [2010-11-16] => Array ( => Array ( [event_date] => 2010-11-16 [title] => Board Meeting ) [1] => Array ( [event_date] => 2010-11-16 [title] => Second event ) ) [2010-11-24] => Array ( => Array ( [event_date] => 2010-11-24 [title] => New meeting ) ) ) Hi all, I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go! I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays. I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too. For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence? Thanks in advance, as above, I've been searching really hard for the answer and believe i'm over my head! Output of print_r($updates); CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #35 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => ) Output of print_r($updates->result_array()); Array ( [0] => Array ( [problem_update_id] => 1 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Some details about a paricular issue [problem_update_active] => 1 ) [1] => Array ( [problem_update_id] => 4 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Another update about the problem with an ID of 3 [problem_update_active] => 1 ) [2] => Array ( [problem_update_id] => 5 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of four [problem_update_active] => 1 ) [3] => Array ( [problem_update_id] => 6 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of 6 [problem_update_active] => 1 ) [4] => Array ( [problem_update_id] => 7 [problem_id] => 3 [problem_update_date] => 2010-10-12 [problem_update_text] => Some new update about the problem with the ID of 3 [problem_update_active] => 1 ) ) I have the following multidimensional array; Array ( => Array ( [value] => Alarm [safe] => Alarm [view] => Alarm ) [1] => Array ( [value] => Deadlocks [safe] => Deadlocks [view] => Deadlocks ) [2] => Array ( [value] => Security Doors [safe] => Security Doors [view] => Security Doors ) ) I'm trying to implode the array to end up with an output like "Alarm, Deadlocks, Security Doors". But I cannot for the life of me work out the correct PHP. I'm assuming i need some foreach loop? Any help would be greatly appreciated. Hi I've set-up a multidimensional PHP array from a SQL query which pulls back parcels despatched per item on a daily basis. e.g. Item|Day1|Day2 |Day3|Day4|Day5 1 |100 | 120 | 90 |150 |60 2 |150 | 200 | 80 |90 |100 3 | 1 | 2 | 3 | 4 | 5 I want to be able to assign a ranked value to each day based upon the amount of items sent out each day and put that either into the existing array, or create a new array to which it can reference Hi I have an array which looks like this when i do a print_r(): Array ( => Array ( [path] => 1 [order] => 2 ) [1] => Array ( [path] => 2 [order] => 1 ) [2] => Array ( [path] => 3 [order] => 2 ) ) In this instance, I want to change the values of the order key. So I do something like this so that the last array has a path of 3 and an order of 1, and the others have orders of 2 foreach ($old_images as $old_image){ if($old_image['path']==3){ $old_image['order']="1"; } else{ $old_image['order']="2"; } } When i step through, i see that the order is changed, but when i do a print_r(), i get the same result. So even though im resetting the order value of the inside arrays, the multidimensional array does not change.... any ideas? I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); What I'm trying to do is take two arrays and combine them using array_combine. After they are combined I want to take each key and value pair and use them in a string to build a MySQL query. See below for some pseudo-code =D <?php $fields = array('first_name', 'last_name'); //the fields that will be set with an UPDATE query. $newvals = array('Joe', 'Blow');//The values that will go into $fields $arr = array_combine($fields, $newvals);//Each value now has a key of the field it will update. /** This is where I want to return my SET string with something like 'SET ' . $arr['first_name'] ' = ' . $arr['first_name_value'] ', etc etc. */ There it is, I suppose. Are there any easier ways of doing what I'm trying to accomplish, that is, am I on the right track or just making things harder for myself? Both arrays will have dynamic values throughout my application, so I need to be able to get both each field and value for each query. Any links to some constructs, etc, etc? That's all I really need and I can post the solution when I've figured it out. I am trying to get rows from MYSQL table (attachment) based on: Select last five rows where status_id = 1 Select last two rows where status_id = 2*I did use UNION with two SELECT queries to get whole data as one array. Now on PHP side... I have first block for rows having status_id 1 so if there are rows having status_id 1 then PHP should display the data otherwise if there is no row having status_id 1 then PHP should print NO DATA only once. I have second block for rows having status_id 2 so if there are rows having status_id 2 then PHP should display the data otherwise if there is no row having status_id 2 then PHP should print NO DATA only once. I did use foreach loop then within loop i did use if condition to check status_id of rows. it works fine when i omit NO DATA part but when i add it. the result shows both rows and NO DATA in first block when there are one or more rows having status_id 1 but no row having status_id 2 Which made me believe that i was doing it the wrong way. Please guide me *PS: I know its PHP section the thread should be related to PHP problem but... is it right to use UNION in query? or should i post a new thread in relevant section for guidance?
Hi guys, This is my problem: I have a csv file let's say have these values: blue,1,2,3,4 red,8,5,6,7 I need to add thes values into a 2d array to be able to use like this: colors[1][2]=2 colors[2][4]=7 Where my first number is the row and the second number is the value within that row. This is what I got so far <?PHP $i=0; $j=0; $file_handle = fopen("color-list.csv", "r"); while (!feof($file_handle) ) { $colors = fgetcsv($file_handle, 10000); for ($j=0; $j<=4; $j++) { $row[$i][$j]=$colors[$j]; print row[$i][$j]; } $i++; ?> For some reason this doesn't work. Maybe I am declaring the arrays wrong. Thank you! Hi - I have an array where one of the values delivers a string. Here is the output from a print_r($pickupdates): Code: [Select] Array ( [0] => Array ( [location] => Clearwater [TheDates] => 1311746700,1321746641,1331668779 ) [1] => Array ( [location] => Canmore [TheDates] => 1311746700,1321746641,1331746641 ) [2] => Array ( [location] => Collingwood [TheDates] => 1311746700,1321746641,1331746641 ) [3] => Array ( [location] => Varsity [TheDates] => 1321746641,1330746700,1331746700 ) The challenge is to get the location and TheDates into a nice HTML table which would look like this: Code: [Select] LOCATION DATE 1 DATE 2 DATE 3 Clearwater April 1 May 1 June 1 Varsity April 2 May 2 June 2 Collingwood April 3 May 3 June 3 Canmore April 4 May 4 June 4 Ok - Now I am stuck, I need to be able to uniquely identify each date that is associated with the location such that I can run a foreach() on then so I can populate the table properly. I tried using list() and explode. List does not separate the dates and explode needs a string not an array. I'm really stuck - a very big Heartfelt thanks to anyone who can steer me in the right direction. MANY THANKS ! Hi all I have a question about how to store parsed data into a multidimensional arrays -- I can store items into an array, but multidimensional arrays mystify me a bit. Any help would be greatly appreciated. I'm storing the images in a table, grouped according to their table and their descriptions, so that later when I call the images, I can display them accordingly. I'm using the Simple HTML DOM Parser. So say I have two tables, one with three images, and another with one. I'd like the resulting to array to look like: Code: [Select] Array ( [0] => Array ( [0] => image1.jpg [1] => 1st Variation Description ) [1] => Array ( [0] => image2.jpg [1] => image3.jpg [2] => image4.jpg [3] => 2nd Variation Description ) ) This is what I have so far: Code: [Select] $html = str_get_html($vartables); $varinfo = array(); foreach($html->find('table') as $table){ $varinfo[] = $table->innertext; } print_r($varinfo); Which yields: Code: [Select] Array ( [0] => <tr> <td width=150> Description1 </td> <td><a href="image1.jpg"> <img src="image1" height=100 border=1></a> </td> </tr> [1] => <tr> <td width=150> Description2 </td> <td><a href="image2.jpg"> <img src="image2.jpg" height=200 border=1></a> </td> <td><a href="image3.jpg"> <img src="image3.jpg" height=200 border=1></a> </td> <td><a href="image4.jpg"> <img src="image4.jpg" height=200 border=1></a> </td> </tr> ) I'd like to strip out the html and keep the .jpg's and descriptions together in a multidimensional array...unfortunately my newbness is getting the better of me there, I'm researching but running into a roadblock. Thanks in advance for any help. I need to set up a threaded comments system in a PHP project and I got this script shown below from http://www.jongales....#comment-436261
class Threaded_comments { public $parents = array(); public $children = array(); /** * @param array $comments */ function __construct($comments) { foreach ($comments as $comment) { if ($comment['parent_id'] === NULL) { $this->parents[$comment['id']][] = $comment; } else { $this->children[$comment['parent_id']][] = $comment; } } } /** * @param array $comment * @param int $depth */ private function format_comment($comment, $depth) { for ($depth; $depth > 0; $depth--) { echo "\t"; } echo $comment['text']; echo "\n"; } /** * @param array $comment * @param int $depth */ private function print_parent($comment, $depth = 0) { foreach ($comment as $c) { $this->format_comment($c, $depth); if (isset($this->children[$c['id']])) { $this->print_parent($this->children[$c['id']], $depth + 1); } } } public function print_comments() { foreach ($this->parents as $c) { $this->print_parent($c); } } }Here’s the example usage with the data provided as an array: $comments = array( array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'), array('id'=>2, 'parent_id'=>1, 'text'=>'Child'), array('id'=>3, 'parent_id'=>2, 'text'=>'Child Third level'), array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'), array('id'=>5, 'parent_id'=>4, 'text'=>'Second Child') ); $threaded_comments = new Threaded_comments($comments); $threaded_comments->print_comments();I have a sample select query that pulls data from a database and stores the result in the $comments array as shown below. The $comments array is then passed as an argument to the $threaded_comments object: $sql = 'SELECT * FROM test_comments'; // submit the query and capture the result $result = $conn->query($sql); $comments = array(); while ($row = $result->fetch_assoc()) { $comments[] = $row; }The challenge is that nothing is printed to the screen when I run the script. Inspection of the comments array with the var_dump function is shown below: array (size=4) 0 => array (size=3) 'id' => string '1' (length=1) 'parent_id' => string '0' (length=1) 'text' => string 'comment' (length=7) 1 => array (size=3) 'id' => string '2' (length=1) 'parent_id' => string '0' (length=1) 'text' => string 'comment' (length=7) 2 => array (size=3) 'id' => string '3' (length=1) 'parent_id' => string '1' (length=1) 'text' => string 'comment ' (length=8) 3 => array (size=3) 'id' => string '4' (length=1) 'parent_id' => string '3' (length=1) 'text' => string 'comment ' (length=8)I was wondering if the array format from my select query is the issue? Could anyone provide a clue as to how to fix this? Thanks. Edited by terungwa, 11 January 2015 - 05:07 PM. Basically, I have a database table called 'users' and I would like to populate a drop down box with these values of 'users'. How?? - to call upon the values is this: 'upduser2' Right now, all I am using is a text box, in where you have to type in the users name manually (this is so an admin can change variables and settings according to that current user). This is what I am using so far: <h3>Update User Level</h3> <? echo $form->error("upduser"); ?> <table> <form action="adminprocess.php" method="post"> <tr> <td> Username:<br /> <input type="text" name="upduser" maxlength="30" value="<? echo $form->value("upduser"); ?>" /></td> <td> Level:<br /> <select name="updlevel"> <option value="1">1 </option> //example settings <option value="9">9 </option> </select></td> <td><br /> <input type="hidden" name="subupdlevel" value="1" /> <input type="submit" value="Update Level" /></td> </tr> </form> </table></td> </tr> Much help would be appreciated. I am trying to extract field data from a specific row, Here is my code: $query = "SELECT * FROM music WHERE title = '%$selected%'"; $musicresult = mysql_query($query) or die ("Couldn't execute query."); $row = mysql_fetch_array($musicresult); /* extract the contents of the row */ Line 98 extract($row); echo $title." "."<BR>"; echo $select."<BR>"; /* set contents of selected to 1 */ $selectval = $select; ++$selectval ; echo $selectval."<BR>"; /* Put the data into the database */ //$query= "UPDATE music SET select=$selectval WHERE title=$selected"; //$result = mysql_query($query) or die ("could not save new data!"); Here is the error message: Warning: extract() [function.extract]: First argument should be an array in /home/content/a/c/k/acksfaq/html/theackshow/THANKYOU.PHP on line 98 The two echo statements that follow line 98 do not generate an output About the only thing that is different with is table compared with a table that this code works with is the fact that this table (music) is not indexed. What the heck am I doing wrong?? Thanks, Ack Any help on this would be greatly appreciated. I am trying to run a mysqldump from my site for my client when they want to back up. They basically will jsut click a button to run the backup. With my script below, the backup file is genrated, but there is no table data in the file. There are the headers in the file with the server IP, linux versin, etc..., but there is no data in the file. Does this look right? Thank you for helping me out. Ryan Code: [Select] ini_set ("display_errors", "1"); error_reporting(E_ALL); $dbhost = 'localhost'; $dbuser = 'databaseuser'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'database'; mysql_select_db($dbname); $tableName = 'lots'; $backupFile1 = '/home/stuff/wwwroot/stuff/appnew/backup/'; $backupFile = $backupFile1.$dbname . date("Y-m-d-H-i-s") . '.gz'; $command = "mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname | gzip > $backupFile"; system($command); $result = mysql_query($command); $example "Q: Example? A:" Let's assume that I'd like to display $example but exclude "Q:" and "A:", how would I do that? I usually only do design work, but a client wanted a log in system to his website, so I decided to do it. I set everything up correctly, and users can sign up, login, and log out. However, he wants to be informed when a user logs into his site. So say (user x) wants logs in, my client wants to receive an email with all of (user x)'s account information. How do I pull a row from mysql based on the login information provided? My database is has 7 columns: user, pass, name, address, state, phone, email I'm *attempting* to write a script that will take a paste of data from a user and when it's submitted it will drop certain parts of the data into databases and be available for recall later. I have the MySQL sorta out I think but I can't test until I get this part done. Basically people will paste a copied paste (CTRL A, CTRL C and go to my form and just hit CTRL V). It will contain a bunch of data I want to drop in the database but in different areas, like the following example the items in bold are the items I want to grab. First Name: Lincoln Last Name: Coe Address: 1234 Easy St, Perfectville, PV 00000 Telephone: 0000000000 Hi there I need to extract data from some XML. I have found a few sites that explain that part to me, however there is a section of the data which I need to extract and am not sure how to go about it. Below is an extract of the XML, the section I am trying to extract is highlighted in red (it is essentially a text message being sent I am trying to get the contents of the sms/text message): <usa_smpp_host>THTTP</usa_smpp_host> <short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message</short_message> <dest_addr_npi>1</dest_addr_npi> Any assistance would be appreciated. Have played around quite a bit but have not managed to figure it out yet. thanks |