I'm been hunting everywhere trying to find how to access data returned via a Zend db call. i want to append each columns value, comma delimited into a variable. I've always used associative calls in the past $row['fieldname'] etc.. but don't want to type out all the fields. I think I'm pretty close with the below but it's not working. Can someone point out my error? Thanks!
$data = $db->fetchAll($query);
$i=13; //number of columns
foreach($data as $row){
for($j=0;$j<$i;$j++) {
$csv_output .= $row[$j].", ";
}
$csv_output .= "\n";
}
Wow, you're over-complicating things! Try:
$csv_output = array();
foreach ($db->fetchAll($query) as $row)
{
$csv_output[] = implode(', ', $row);
}
$csv_output = implode("\n", $csv_output);
echo '<pre>';
print_r($csv_output);
echo '</pre>';
Related
I am trying to populate a drop-down menu with values from an array.
I have tried to follow other answers but the syntax doesn't seem to be working. (I'm still relatively new to PHP).
The following code which I am working on was produced by someone else.
$sqlite_query = "SELECT * FROM dis_kind";
$result = $db->query($sqlite_query);
$array = $result->fetchArray();
$output = "<select name=\"kind\" class=\"dis\" >\n";
$output .= "<option value=\"$this->wildcard_value\"></option>\n";
foreach ($result as $array) {
$value = $array['kind'];
$output .= "<option value=\"";
$output .= $value;
$output .= "\">";
$output .= $value;
$output .= " - ";
$output .= $array['description'];
$output .= "</option>\n";
}
$output .= "</select>\n";
I don't know why it has been done the way it has but I am stumped as to getting my drop-down to work.
Currently, the box appears but is populated with no values.
Thanks.
Eventually managed to solve the problem myself - before I pulled my hair out!
Instead of using the foreach loop, I used the following:
while($array = $result->fetchArray())
{
// Output code.
}
Which populated the drop-down with values from the array.
I have made a PHP file that takes this JSON-based file: http://www.yellowpages.com.iq/ajax/get/register/Categories.php
and converts it into an excel-formatted-table with the formatting of (.cvs) using this code :
$contents = file_get_contents('http://www.yellowpages.com.iq/ajax/get/register/Categories.php');
$data = JSON_decode($contents);
$excel_file = fopen("file.csv", "a+");
$table = "<table border='1'>";
foreach($data as $elem) {
$table .= "<tr>";
foreach($elem as $key => $prop) {
$table .= "<th>$key</th>";
$table .= "<td>$prop</td>";
fwrite($excel_file, "$key,$prop\n");
}
$table .= "</tr>";
}
$table .= "</table>";
echo $table;
But the problem being, is it takes the data and displays it correctly, although it tends to format it like so: id 1
category Advertising
id 2
category Agriculture & FoodÂ
id 3
category Air condition
id 4
category Airlines
id 5
Aluminium & Glass
Instead of what I'm trying to make it look like which I made manually:
Any help would be appreciated!
You could change the code using fputcsv, which takes care of double quotes and escaping them.
For that you need to get the JSON as an associative array (provide the second argument true):
$data = JSON_decode($contents, true);
And then the loop you have would be replaced with this:
// "loop" for the header (only 1 iteration)
foreach($data as $elem) {
$table .= "<tr><th>" . implode("</th><th>", array_keys($elem)) . "</th></tr>";
fputcsv($excel_file, array_keys($elem));
break; // only need one row for header
}
// Restart loop for the data
foreach($data as $elem) {
$table .= "<tr><td>" . implode("</td><td>", $elem) . "</td></tr>";
fputcsv($excel_file, $elem);
}
Why not output the first element array?
i use next code
$product_idn='123112$2313213';
$count_products=substr_count($product_idn,'$')+1;
$idn_products=explode('$',$product_idn);
$name_products='';
$s=0;
while($s<=$count_products){
$prod=$idn_products[$s];
$res10=mysql_query("..... WHERE `product_idn`='$prod'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
$s++;
}
echo $name_products;
//give 2313213,,
Why not output the first element array ?
What about
$product_idn='123112$2313213';
$idn_products=explode('$',$product_idn);
$name_products='';
foreach($idn_products as $val){
$res10=mysql_query("..... WHERE `product_idn`='$val'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
}
echo $name_products;
There are a lot of unusual techniques being used in the original code. My best guess at what I'd do, without really knowing the purpose of this code is:
$product_idn = '123112$2313213';
$idn_products = explode('$', $product_idn);
$name_products = '';
foreach($product_idn as $value) {
$res10 = mysql_query("SELECT name FROM ... WHERE `product_idn`='$value'");
if ($res10) {
$i10 = mysql_fetch_assoc($res10);
$name_products .= $i10['name'].', ';
}
}
$name_products = rtrim(', ', $name_products);
echo $name_products;
I have a field in my database with the text value:
"these, are, some, keywords" (minus the inverted commas)
Now, I wonder if I can generate an unordered list from this so ultimately my HTML reads:
<ul>
<li>these</li>
<li>are</li>
<li>some</li>
<li>keywords</li>
</ul>
Is this possible with PHP and if so is anyone able to help me out with this?
Many thanks for any pointers.
You can accomplish this with something like the following:
<?php
$yourList = "these, are, some, keywords";
$words = explode(',', $yourList);
if(!empty($words)){
echo '<ul>';
foreach($words as $word){
echo '<li>'.htmlspecialchars($word).'</li>';
}
echo '</ul>';
}
?>
As mentioned by elcodedocle, you may want to use str_getcsv() instead of explode if more appropriate.
Have a look at str_getcsv() and explode()
Example:
<?php
$mystring = "these, are,some , keywords";
$myvalues = str_getcsv($mystring);
$myoutput = "<ul>";
foreach ($myvalues as $value){
$myoutput .= "<li>".trim($value)."</li>\n";
}
$myoutput .= "</ul>";
echo $myoutput;
?>
You need to explode you string for ', '
print <ul>
for each element in the array you received you print '<li>' . $value . '</li>'
print </ul>
You can try:
$arr = explode(",","these, are, some, keywords");
$res = "<ul>";
foreach ($arr as $val){
$res .= "<li>" . $val . "</li>";
}
$res .= "</ul>";
echo $res;
Here's my code:
$values = mysql_query("SELECT cult_desc FROM culture");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;
As far as I can tell, it should go through each piece of data, echo it with a ";" and then a newline. Instead, it gives me no output.
From the variable name ($csv_output) it seems you need CSV formatted output.
If you have FILE privilege why not invoke,
$values = mysql_query("SELECT cult_desc INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
FROM culture");
readfile("/tmp/result.txt");
Otherwise following code will do it.
$values = mysql_query("SELECT cult_desc FROM culture");
$csv_output = "";
while ($rowr = mysql_fetch_row($values)) {
$csv_output .= implode(";", $rowr). "\n";
}
$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;
Try initializing $csv_output with an empty string so it has something to concatenate onto. Otherwise you may get a notice, and your code won't work. Additionally, check to make sure there are actually values in your results.
$csv_output = '' ought to do the trick, call that before your loop unless you initialize it elsewhere.
You also need to replace $i with $j unless you have $i declared elsewhere?
First of all mysql_fetch_row has in your case just one key its 0 (there the cult_desc field is stored).
You can use your code like this:
$csv_output = "";
$values = mysql_query("SELECT cult_desc FROM culture");
while ($rowr = mysql_fetch_assoc($values)) {
$csv_output .= $rowr['cult_desc']."; \n";
}
$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;
i replaced mysql_fetch_row with mysql_fetch_assoc so you get the field name as the key not a number. With that you can direct access with cult_desc