PHP - Take Comma Seperated Values From One Array And Put Them In A New Array
I need to some how pull comma separated images $data[23] from this $data array and then put them into there own array so I can insert them into separate rows in a database.
This is the provided array: $data[0] => VIN $data[1] => StockNumber ...(all the other number here) $data[23] => Image_URLs (comma seperated) $data[24] => Equipment // I need something like this (obviously doesn't work?!@#) $delimiter = ","; $img = explode($delimiter, $data[23]); foreach($img as $pic){ $sqlPic = "insert into class_prodimages (pid image rank) values('".$LastId['id']."', '$pic', '".rand(1,20)."')"; } Any help here? Similar TutorialsHello all, I'm trying to get better at manipulating arrays and I'm stumped on this one. What would be the most efficient way of converting an array such as this: Quote Array ( [3] => 3 [9] => 1 [15] => 9 ) Into this: Quote 3,3,3,9,15,15,15,15,15,15,15,15,15 I am trying to give role based access here. I want to display the pages which user has access in checkbox format. Here is my code $sql = "SELECT p.page_id AS pid, p.page, p.href, ra.pages AS rpage FROM pages p INNER JOIN role_access ra WHERE p.page_id IN (ra.page) AND ra.role=1"; $query = mysqli_query($con, $sql) or die(mysqli_error($con)); $checked_arr = array(); while($row = mysqli_fetch_array($query)) { $checked_arr = explode(",",$row['rpage']); foreach ($checked_arr as $page) { echo "<br/><input type='checkbox' name=\"pages[]\" value='$page' />$page<br>"; } My tables are like this ROLE CREATE TABLE `role` ( `rid` int(5) NOT NULL, `role_name` varchar(50) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `role` (`rid`, `role_name`) VALUES (1, 'Admin'), (2, 'Others'); ALTER TABLE `role` ADD PRIMARY KEY (`rid`); ROLE-ACCESS CREATE TABLE `role_access` ( `id` int(10) NOT NULL, `page` varchar(160) NOT NULL, `role` int(7) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `role_access` (`id`, `page`, `role`) VALUES (1, '1,2,3,4,5', 1), (2, '2,4,5', 2); ALTER TABLE `role_access` ADD PRIMARY KEY (`id`); PAGES CREATE TABLE `pages` ( `page_id` int(11) NOT NULL, `code` varchar(10) NOT NULL, `page` varchar(100) NOT NULL, `href` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `pages` (`page_id`, `code`, `page`, `href`) VALUES (1, 'Patient', 'Registration', '/patient_registration.php'), (2, 'Patient', 'List', '/patient_list.php'), (3, 'Patient', 'Edit', '/edit_patient.php'), (4, 'Patient', 'Export', '/export_patient.php'), (5, 'Patient', 'MRD', '/patient_MRD.php'); ALTER TABLE `pages` ADD PRIMARY KEY (`page_id`);
In above query i get result like this
But required result is
if i change checkbox display like while($row = mysqli_fetch_array($query)) { $checked_arr = explode(",",$row['rpage']); $pname = $row['page']; foreach ($checked_arr as $page) { echo "<br/><input type='checkbox' name=\"pages[]\" value='$page' />$pname<br>"; } } i get result
How to get the names for that file.
How can i save array from inputs, witch is $igraci and it have 5 values, to mysql base, i tryed but in mysql it shows Array. This is form: <form action="dodaj_klan.php" method="post" > <p> <label>Naziv klana:</label> <input name="naziv" type="text" size="20%" /> <label>Web Sajt:</label> <input name="website" type="text" size="20%" /> <label>E-Mail:</label> <input name="email" type="text" size="20%" /> <br /><br /> <label>Igraci klana(5):</label> <input name="lider" type="radio" value="1" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="2" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="3" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="4" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="5" /> <input name="igraci[]" type="text" size="20%" /> <label><i>(obelezi lidera klana)</i></label> <br /><br /> <input class="button" type="submit" name="submit" value="Dodaj" /> </p> </form> Now i wonna save this igraci[] array in mysql, i tried like this but it doesn't work: $igraci = $_POST['igraci']; $query = "INSERT INTO klanovi (naziv, website, email, igraci) VALUES ('{$naziv}', '{$website}', '{$email}', '{$igraci}')"; $result = mysql_query($query, $connection); How can i do this? Thanks.. So I'm trying to create an Array from a comma separated string containing array{s}. How can I take a string full of comma separated hex values such as : <?php $myStr = ( "00ffff", "00cccc", "009999", "007777", "004444", "001111", "01ffff", "01cccc", "019999" ); ?> & turn it into 3 separated arrays like : <?php $myNewArrs = [ Array1 ( 3 ), Array2 ( 3 ), Array3 ( 3 ), ]; ?> which REALLY looks like : <?php $myNewArrs = [ Array ( "00ffff", "00cccc", "009999" ), Array ( "007777", "004444", "001111" ), Array ( "01ffff", "01cccc", "019999" ), ]; ?>
? Thank you & have a great afternoon!
Edited May 27 by AquariaXI I have a field in my MySQL DB that holds numbers separated by a comma (example: 1, 3, 45, 137, 87). I simply want to get a count of how many numbers are in there. (Using my example the result would be 5). I'm sure this is a simple question but I don't know how. I'm able to extract the numbers but I can't get the count to read anything other than 1. Can anyone help? Hi, I am trying to find a way that will allow me to convert information passed via POST into array so I can run foreach on the array. For example if information in $_POST[ids] is 1000, 1001, 1002, 1003 I would like to get it into an array $array = array("1000", "1001", "1002", "1003") Thanks Hello all, I have yet again trouble finding a logical solution to my problem. I'm fetching an array which can hold 1 or more values. The problem is, I want these values to ouput in my json_encode function, but this also needs to happen dynamically depending on the amount of values. I can't explain it further, so here's the code so far: Code: (php) [Select] $files = mysql_fetch_array($get_files); $a = count($files); $i = 1; while ($files) { $variablename = 'fileName' . $i; $$variablename = $files['fileName']; $i++; } $output = array( OTHER VALUES , 'fileName1' => $fileName1, 'fileName2' => $fileName2, 'fileName3' => $fileName3, ............); // How do I add the fileNames dynamically depending on how many there are? This got me thinking, I also need to dynamically GET the values with jQuery. How would I do that, when the above eventually works? Thank you. Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. I have mysql select query shown below retrieving data from a database:
$sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } }On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3)How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback: Warning: max(): When only one parameter is given, it must be an array Thanks. I need to loop through my db and create strings from 2 columns on my db. data in column1 is '22,33,33,33,25,25,25,14,14,14,14,14,14' data in column2 is '11am,12pm,1pm,2pm,3pm,4pm,5pm,6pm,7pm,8pm,9pm,10pm, 11pm' the end result I need is a string such as: $body= "11am, 22 <line break> 12pm, 33 <line break> ...... If anyone has any examples of something like this or can get me started it would be much appreciated. How can i convert an array of results to a single comma separated string so that I can run it through an SQL query? 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]); I have an array created to control the down states of the my buttons in my navigation. Which is working well. This array will have quite a few strings in it before I'm done. I then want to conditionally load a CSS file for all the files in that array with the exception of two, is this possible? In plain in english I want to say If in array apart from these values then load my css file. My array (will have more values when complete) Code: [Select] $otherTitles = array('Product Selector', 'Avon Range', 'Knightsbridge Range', 'Kingston Range' ); My code to load a css file Code: [Select] <?php if (in_array($title, $otherTitles)) { ?> <link rel="stylesheet" type="text/css" href="homepage.css"/> <?php } ?> I want all titles in the otherTitles array to get this CSS file apart from two Product Selector and Avon Range. Thanks Richard Hello there again!
I have the following construct of array now (schematics):
array[0] array[id] = "1" array[title] = "title" array[1] array[id] = "3" array[title] = "another title"Now what i want to do, is to assign the parent-array keys to the IDs of it's contents. This is what it should look like: array[1] array[id] = "1" array[title] = "title" array[3] array[id] = "3" array[title] = "another title"How do I do that again? I know I did this some years ago, but I can not remember how. 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'm trying to split a url into segments so that a url would be stored in an array. Example: A url like this http://localhost/application/blog/view/Test-Blog-Entry/1 Would look like this in an array: Code: [Select] array('blog', 'post', 'Test-Blog-Entry', '1'); It's an MVC url so 'blog' would be controller, 'post', would be method and the last two would be variables. I want to display the variable values of the array. Is there an easy way to do this? Basically I am pulling data from an XML feed on wowarmory.com Everything is working as far as getting the item names and even a list of the item levels. What I am trying to do now is add together all of the item levels in to one value that I will call $calc (I will then divide this by 16 to find the average item level). I am using a foreach loop to echo the Item Name and it's corresponding item level. I am now trying to grab the item level on each loop so I can add them all up and store them in $calc for further manipulation outside of the loop. I am a complete novice but learning, How would I go about achieving this? Of course the code I have atm is re-defining $calc every loop, so naturally it isn't working. Here is my current code: //Get the UID for each item the character has //Then look up the name for that item foreach($char_xml->characterInfo->characterTab->items->item as $item) { //get the item id $item_id = $item->attributes()->id; //get the xml doc for that item from wow armory $url = "http://www.wowarmory.com/item-info.xml?i=" . $item_id; $data = fetchXML($url); //create a SimpleXML object to parse the xml $item_xml = new SimpleXmlElement($data); //print the item's name echo "<b> Item Name: </b>" . $item_xml->itemInfo->item->attributes()->name . "</br>"; echo "<b><i> iLvL: </i></b>" . $item_xml->itemInfo->item->attributes()->level . "</br>"; //obviously this is wrong, because It is redefining $calc every loop $calc=$item_xml->itemInfo->item->attributes()->level; } //print the total item level echo $calc; How do you get the values in an array and name them as variables? Lets say I want the 0 and the 2... Array ( => 02.02.2011 [1] => 97% [2] => 538 [3] => 20 [4] => 63,247 (2.20) [5] => 77,639 [6] => 195,617 [7] => 09.13.2010 ) I am working in wordpress and things are going good. I made a form, filled it with values and have been able to store those values in a database using a wordpress function. Now my issue is that I want learn how to write them into an array and store them that way. I have been looking at several different ways but can't figure out how to code it. This is my code now Code: [Select] function add_slider_box(){ global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imagecap', true ); ?> <div style="width: 50%" class="sliderbox"> <p><label for="imageurl">Image Url: <br /> <input type="text" class="widefat" id="imageurl" name="imageurl" value="<?php if( $imageurl ) { echo $imageurl; } ?>" /></label></p> <p><label for="imagelink">Image Link: <br /> <input type="text" class="widefat" id="imagelink" name="imagelink" value="<?php if($imagelink) { echo $imagelink; }?>" /></label></p> <p><label for="imagecap">Image Caption: <br /> <input type="text" class="widefat" id="imagecap" name="imagecap" value="<?php if( $imagecap ) { echo $imagecap; } ?>" /></label></p> <button class="remove_slide button-secondary">Remove This Slide</button> </div> <?php } function bigBusiness_save_slider_options($post_id){ global $post; if( $_POST ) { update_post_meta( $post->ID, 'imageurl', $_POST['imageurl'] ); update_post_meta( $post->ID, 'imagelink', $_POST['imagelink']); update_post_meta( $post->ID, 'imagecap', $_POST['imagecap'] ); } } so the update_post_meta function is what is saving the information into the database. How do I put that into an array? I was thinking Code: [Select] foreach($_POST as $slides) $slides[] = array( 'imageurl' => '$_POST['imageurl'], 'imagelink' => $_POST['imagelink'], 'imagecap' => $_POST['imagecap']; ); I have been able to use the data out of the database by creating a function Code: [Select] function get_half_slider_slides() { global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imageurl', true ); return array( $imageurl, $imagelink, $imagecap ); } and then calling that function in my code like this $slide = get_half_slider_slides(); I thought since it returned an array that I would be able to do this $slides['imagelink'] $slides['imageurl'] but it doesn't work. What I am trying to do is populate the nivo-slider ( http://nivo.dev7studios.com ) with images from the database. I have been following a tutorial but since the license on the tutorial doesn't cover reusing the code, I am trying to rewrite it in a way that doesn't violate the license agreement. This code is so far is hacked together but completely different from the tut. Can you help? Thanks Hi, I have this $_POST array coming from a paypal transaction Code: [Select] Array ( [address_city] => San Jose [address_country] => United States [address_country_code] => US [address_name] => Test User [address_state] => CA [address_status] => confirmed [address_street] => 1 Main St [address_zip] => 95131 [business] => test1_biz@gmail.com [charset] => windows-1252 [custom] => [first_name] => Test [item_name1] => Product Name 1 [item_name2] => Product Name 6 [item_name3] => Product Name 8 [item_number1] => 1 [item_number2] => 6 [item_number3] => 8 [last_name] => User [mc_currency] => USD [mc_fee] => 0.82 [mc_gross] => 18.00 [mc_gross_1] => 14.00 [mc_gross_2] => 2.00 [mc_gross_3] => 2.00 [mc_handling] => 0.00 [mc_handling1] => 0.00 [mc_handling2] => 0.00 [mc_handling3] => 0.00 [mc_shipping] => 11.00 [mc_shipping1] => 11.00 [mc_shipping2] => 0.00 [mc_shipping3] => 0.00 [notify_version] => 3.4 [num_cart_items] => 3 [payer_email] => test_biz@gmail.com [payer_id] => TRCLJTHLNCJ7Q [payer_status] => verified [payment_date] => 22:52:56 Jan 27, 2012 PST [payment_fee] => 0.82 [payment_gross] => 18.00 [payment_status] => Completed [payment_type] => instant [protection_eligibility] => Eligible [quantity1] => 1 [quantity2] => 1 [quantity3] => 1 [receiver_email] => test_biz@gmail.com [receiver_id] => 74PV23B8KSK84 [residence_country] => US [tax] => 0.00 [tax1] => 0.00 [tax2] => 0.00 [tax3] => 0.00 [test_ipn] => 1 [transaction_subject] => Shopping CartProduct Name 1Product Name 6Product Name 8 [txn_id] => 7BS85664SB906284D [txn_type] => cart [verify_sign] => AJ2IuqHp8G0lIxhedAqrwRQbv8fVA4-Gum9e7DMZmEWIFrljEwFCJDfP ) 1 as you can see, there are key value pairs like this, Code: [Select] [items_name1] => Product Name 1 [items_name2] => Product Name 2 [items_name3] => Product Name 3 those keys aren't constant, I mean, it came from a shopping cart, so the item name depends on how many items were placed in the shopping cart to be purchased.. now my question is, How to loop to that post array, and get only the item_names plus their corresponding values ?, because I need them and save in a db table |