PHP - Comma Separated Data In Db - > Array
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. Similar TutorialsHow 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.. How can i convert an array of results to a single comma separated string so that I can run it through an SQL query? Hello 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 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 Hi, I have in column "keywords" comma separated keywords in one row/news. Example in ID "1" in column "keywords" have: "Šport, Fudbal, Četvrta Liga". I want to make a tag-s from those words to be like: http://site.com/tag/sport, http://site.com/tag/fudbal, http://site.com/tag/cetvrta-liga. At first I separate those words by: $input = $vest_keywords; $removed_spaces = preg_replace('/\s*,\s*/', ',', $input); $output = explode(',', $removed_spaces); foreach($output as $tags) { echo $tags; } This give me: Šport | Fudbal | Četvrta liga
But I have a "slug-maker", and I have a problem with redeclare. /* SLUG */ $string = $output; function my_str_split($string) { $slen = strlen($string); for ($i = 0; $i < $slen; $i++) { $sArray[$i] = $string{$i}; } return $sArray; } function noDiacritics($string) { //cyrylic transcription $cyrylicFrom = array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я'); $cyrylicTo = array('A', 'B', 'W', 'G', 'D', 'Ie', 'Io', 'Z', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'Ch', 'C', 'Tch', 'Sh', 'Shtch', '', 'Y', '', 'E', 'Iu', 'Ia', 'a', 'b', 'w', 'g', 'd', 'ie', 'io', 'z', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ch', 'c', 'tch', 'sh', 'shtch', '', 'y', '', 'e', 'iu', 'ia'); $from = array("Á", "À", "Â", "Ä", "Ă", "Ā", "Ã", "Å", "Ą", "Æ", "Ć", "Ċ", "Ĉ", "Č", "Ç", "Ď", "Đ", "Ð", "É", "È", "Ė", "Ê", "Ë", "Ě", "Ē", "Ę", "Ə", "Ġ", "Ĝ", "Ğ", "Ģ", "á", "à", "â", "ä", "ă", "ā", "ã", "å", "ą", "æ", "ć", "ċ", "ĉ", "č", "ç", "ď", "đ", "ð", "é", "è", "ė", "ê", "ë", "ě", "ē", "ę", "ə", "ġ", "ĝ", "ğ", "ģ", "Ĥ", "Ħ", "I", "Í", "Ì", "İ", "Î", "Ï", "Ī", "Į", "IJ", "Ĵ", "Ķ", "Ļ", "Ł", "Ń", "Ň", "Ñ", "Ņ", "Ó", "Ò", "Ô", "Ö", "Õ", "Ő", "Ø", "Ơ", "Œ", "ĥ", "ħ", "ı", "í", "ì", "i", "î", "ï", "ī", "į", "ij", "ĵ", "ķ", "ļ", "ł", "ń", "ň", "ñ", "ņ", "ó", "ò", "ô", "ö", "õ", "ő", "ø", "ơ", "œ", "Ŕ", "Ř", "Ś", "Ŝ", "Š", "Ş", "Ť", "Ţ", "Þ", "Ú", "Ù", "Û", "Ü", "Ŭ", "Ū", "Ů", "Ų", "Ű", "Ư", "Ŵ", "Ý", "Ŷ", "Ÿ", "Ź", "Ż", "Ž", "ŕ", "ř", "ś", "ŝ", "š", "ş", "ß", "ť", "ţ", "þ", "ú", "ù", "û", "ü", "ŭ", "ū", "ů", "ų", "ű", "ư", "ŵ", "ý", "ŷ", "ÿ", "ź", "ż", "ž"); $to = array("A", "A", "A", "AE", "A", "A", "A", "A", "A", "AE", "C", "C", "C", "C", "C", "D", "D", "D", "E", "E", "E", "E", "E", "E", "E", "E", "G", "G", "G", "G", "G", "a", "a", "a", "ae", "ae", "a", "a", "a", "a", "ae", "c", "c", "c", "c", "c", "d", "d", "d", "e", "e", "e", "e", "e", "e", "e", "e", "g", "g", "g", "g", "g", "H", "H", "I", "I", "I", "I", "I", "I", "I", "I", "IJ", "J", "K", "L", "L", "N", "N", "N", "N", "O", "O", "O", "OE", "O", "O", "O", "O", "CE", "h", "h", "i", "i", "i", "i", "i", "i", "i", "i", "ij", "j", "k", "l", "l", "n", "n", "n", "n", "o", "o", "o", "oe", "o", "o", "o", "o", "o", "R", "R", "S", "S", "S", "S", "T", "T", "T", "U", "U", "U", "UE", "U", "U", "U", "U", "U", "U", "W", "Y", "Y", "Y", "Z", "Z", "Z", "r", "r", "s", "s", "s", "s", "ss", "t", "t", "b", "u", "u", "u", "ue", "u", "u", "u", "u", "u", "u", "w", "y", "y", "y", "z", "z", "z"); $from = array_merge($from, $cyrylicFrom); $to = array_merge($to, $cyrylicTo); $newstring = str_replace($from, $to, $string); return $newstring; } function makeSlugs($string, $maxlen = 0) { $newStringTab = array(); $string = strtolower(noDiacritics($string)); if (function_exists('str_split')) { $stringTab = str_split($string); } else { $stringTab = my_str_split($string); } $numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-"); //$numbers=array("0","1","2","3","4","5","6","7","8","9"); foreach ($stringTab as $letter) { if (in_array($letter, range("a", "z")) || in_array($letter, $numbers)) { $newStringTab[] = $letter; } elseif ($letter == " ") { $newStringTab[] = "-"; } } if (count($newStringTab)) { $newString = implode($newStringTab); if ($maxlen > 0) { $newString = substr($newString, 0, $maxlen); } $newString = removeDuplicates('--', '-', $newString); } else { $newString = ''; } return $newString; } function checkSlug($sSlug) { if (preg_match("/^[a-zA-Z0-9]+[a-zA-Z0-9\-]*$/", $sSlug) == 1) { return true; } return false; } function removeDuplicates($sSearch, $sReplace, $sSubject) { $i = 0; do { $sSubject = str_replace($sSearch, $sReplace, $sSubject); $pos = strpos($sSubject, $sSearch); $i++; if ($i > 100) { die('removeDuplicates() loop error'); } } while ($pos !== false); return $sSubject; } $vesti_tag_slug = makeSlugs($string, $maxlen = '0'); /* /. SLUG */ So I want to have tags like: sport | fudbal | cetvrta-liga Can You help how can I get the links that You se above? Thanks in advanced! T So i have a blog that has the tags store in one field in my blog table. So tags are saved as this in each blog post with its respective tags.
Tags: john, carlos, the great tag, this is a tag
What i want to do is get random tags from each blog post and display them. I am not sure how i can begin to make this query. Do you guys have an idea on how to do this?
I have a file that is uploaded by the user that has a series of rows of data in an html table. The columns are always a constant, but the number of rows in the file can change. These rows of the html table are the only information in the file when I am processing it. I need to either convert the file to a CSV file or write the values to a CSV list so that I can then insert them into a database. Any suggestions would be wonderful...I've spent the last three hours spinning my wheels. Here is an example of one row of data from the file: Code: [Select] <tr><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT > </td><td bgcolor=#dddddd class=dataFT > </td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >1015020</td><td bgcolor=#dddddd class=dataFT >155498322</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >4731751</td><td bgcolor=#dddddd class=dataFT >2011-09-25 11:18:33 (-5:00)</td><td bgcolor=#dddddd class=dataFT >2011-09-25 12:16:50 (-5:00)</td><td bgcolor=#dddddd class=dataFT >0:58:17</td><td bgcolor=#dddddd class=dataFT >0:58:16</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >Resolved</td></tr> This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=342695.0 Is there a way to take data that is listed like a,b,c,d,e and turn that into an unordered list? Basically, I'm going to have a list of schools that will be listed with comma separators. I'll print that data out in two places, one will be printed as it's in the database. The other I would like to echo as an unordered list. The following code returns thousands of results separated by a semicolon; Code: [Select] while($getpostid_result=mysql_fetch_array($getpostid)) { $postusername=$getpostid_result['username']; echo $postusername.";"; } the problem is it does not print everything in one row.it breaks into the nextline after if it encounters certain chatacters or some other reason. I want it to able to print everything in one row irrespective of the number of results returned. Hi Guys I'm returning to programming after 5 years, almost a newbie. I have a field(column) in a MySQL database table that has strings of characters separated by a delimiter (||). For example: (TOM||PAUL||HARRIS) I would like to separate the strings into an array. Something like this: ARRAY[0] = 'TOM'; ARRAY[1]='PAUL'; ARRAY[3]='HARRIS'; Can some one tell me how to do this? TIA. 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? 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.
I'm creating a tool for building project proposals for clients, with pricing. I have a database with two tables: services and clients. The services table looks like this: Code: [Select] | code | name | cost | +-------------+-------------+------+ | logo_design | Logo design | 10 | | web_design | Web design | 20 | The clients table looks like this: Code: [Select] | id | client | logo_design | web_design | +----+---------+-------------+------------+ | 1 | Walrus | yes | yes | | 2 | Narwhal | no | yes | How would I link the results from these two tables so that I only pull out the services each person wants from the database? Here is what I have so far: Code: [Select] <? $sql = "SELECT * FROM services"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { echo $row{'name'} . ': $' . $row{'cost'} . '<br />'; } ?> This, of course, displays all the services, with all the prices, like so: Logo design: $10 Web design: $20 I want to add something here so that based on the selected client, we see only the services/prices they selected (ol' Narwhal's page would not show the "Logo Design" line). I've got the data from "clients" pulled in to this page as well, but don't know how to relate it to "services." Thanks in advance! Let me know if I've left out any info that would help you to point me in the right direction. Hi, after banging my head against the wall for a while thinking this would be a simple task, I'm discovering that this is more complicated than I thought. Basically what I have is a link table linking together source_id and subject_id. For each subject there are multiple sources associated with each. I had created a basic listing of sources by subject... no problem. I now need a way of having a form to create an ordered list in a user-specified way. In other words, I can currently order by id or alphabetically (subject name lives on a different table), but I need the option of choosing the order in which they display. I added another row to this table called order_by. No problem again, and I can manage all of this in the database, however I want to create a basic form where I can view sources by subject and then enter a number that I can use for sorting. I started off looping through each of the entries and the database (with a where), and creating a foreach like so (with the subject_id being grabbed via GET from the URL on a previous script) Code: [Select] while($row = mysqli_fetch_array($rs)) { //update row order if (isset($_POST['submit'])) { //get variables, and assign order $subject_id = $_GET['subject_id']; $order_by = $_POST['order_by']; $source_id = $row['source_id']; //echo 'Order by entered as ' . $order_by . '<br />'; foreach ($_POST['order_by'] as $order_by) { $qorder = "UPDATE source_subject set order_by = '$order_by' WHERE source_id = '$source_id' AND subject_id = '$subject_id'"; mysqli_query($dbc, $qorder) or die ('could not insert order'); // echo $subject_id . ', ' . $order_by . ', ' . $source_id; // echo '<br />'; } } else { $subject_id = $_GET['subject_id']; $order_by = $row['order_by']; $source_id = $row['source_id']; } And have the line in the form like so: Code: [Select] echo '<input type="text" id="order_by" name="order_by[]" size="1" value="'. $order_by .'"/> (yes I know I didn't escape the input field... it's all stored in an htaccess protected directory; I will clean it up later once I get it to work) This, of course, results in every source_id getting the same "order_by" no matter what I put into each field. I'm thinking that I need to do some sort of foreach where I go through foreach source_id and have it update the "order_by" field for each one, but I must admit I'm not sure how to go about this (the flaws of being self-taught I suppose; I don't have anyone to go to on this). I'm hoping someone here can help? Thanks a ton in advance hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. Using curl_multi, I have loaded up 3 url's and then printed the array. 1) How can I also set a timestamp on output to let me know when each url was run? 2) How can I explode the array to only display the data and timestamp, excluding the text "Array ( =>", "[1] =>", "[2] =>" and " )"? Code <?php function multiRequest($data, $options = array()) { // array of curl handles $curly = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data and create curl handles // then add them to the multi-handle foreach ($data as $id => $d) { $curly[$id] = curl_init(); $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL, $url); curl_setopt($curly[$id], CURLOPT_HEADER, 0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); // post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST, 1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); } } // extra options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute the handles $running = null; do { curl_multi_exec($mh, $running); } while($running > 0); // get content and remove handles foreach($curly as $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // all done curl_multi_close($mh); return $result; } $data = array(array(),array()); $data[0]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json'; $data[1]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Black+Eyed+Peas&output=json'; $data[2]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Nirvana&output=json'; $r = multiRequest($data); print_r($r); ?> Output Array ( => Pearl Jam [1] => Black Eyed Peas [2] => Nirvana ) Preferred Output 01:00:01 Pearl Jam 01:00:02 Black Eyed Peas 01:00:03 Nirvana This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=328435.0 Hello, I have a variable called $Price, We are getting it through Mysql Database using While loop. We getting the data from database in ordered by ID. Now then i have requirement to store that data into Low to High form ... Like we are receiving $price lke unordered form .. 50 14 35 25 00 145 52 Here i just want to store it in Low to high form like 00, 14,25,35 ... and so on .. Please suggest me the appropriate code. While($myrow=mysql_fetch_array($result, MYSQL_ASSOC)) { some codes return value $price. // want to store in array } $array($price) // here want to store in Low to high with key value. Say you have a string that you know has two, and exactly two, periods in it, like this...
$string = "3.12.8"How can you grab the three different values that are separated by periods (in this case, 3, 12 and ? I was thinking use strpos to find the position of the first period, then could use that info to grab everything up to that position. But then I couldn't figure out how you would find the position of the SECOND period. And even if I could get the exact positions of the two periods, it seems like there should be an easier way to simply retrieve values separated by specific characters. Is there a specific function for this type of thing, or was my initial though correct with having to do it in two parts essentially (find positions of periods, then use that knowledge to get the values)? Thanks! |