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.
Related
I am trying to make a table that has different colors for each of its columns. There will be four columns.
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$html .= '<tr style="background-color:white;">';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
I created an array called colors that has the strings of the colors I want, but I don't know how to put that into the tag. I tried typing it in there, but since it is a string, it takes it as text instead of as code. Where it says "background-color:white;", I'd like it to call the values from the array instead. Thanks.
You can array_pop for this provided you have exactly 4 columns. You also can't apply background colours to <tr> like that, you need to apply them to the <td>
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$color = array_pop($colors);
$html .= "<tr>";
foreach($value as $key2=>$value2){
$html .= "<td style='{$color}'>" . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
I am trying to select a previously selected value inside dropdown selection using php. My code is as follows
<?php
$selected_val = "Assistant Professor";
function generateSelect($type = 'text', $name = 'desgntn', $id = 'fac_dsgn', $options = array(), $default) {
$html = '<select type="'.$type.'" name="'.$name.'" id="'.$id.'">';
foreach ($options as $option ) {
if ($option == $default) {
$html .= '<option value='.$option.' selected="selected">'.$option.'</option>';
} else {
$html .= '<option value='.$option.'>'.$option.'</option>';
}
}
$html .= '</select>';
return $html;
}
$list = array("Lecturer", "Assistant Professor", "Associate Professor", "Professor", "Assistant Professor (On Leave)", "Associate Professor (On Leave)", "Professor (On Leave)");
$res = generateSelect('text', 'desgntn', 'fac_dsgn', $list, $selected_val);
echo $res;
?>
But the problem is I am always getting the first word of a multi spaced option inside value attributein option tag of html. for example: I am getting Assistant inside value tag for Assistant Professor. Is there any way so that I can always get the actual value with spaces?
I believe you forgot to enclose the option value in quotes:
it should be something like
$html .= '<option value="'.$option.'" selected="selected">'.$option.'</option>';
(note the double quotes around $option)
Hope it helps
I'm trying to create an overlay for my twitch stream, which display not only the recent follower but a certain amount of them in chronological order.
Here a visual representation:
(Styling is done with HTML/CSS)
I've already managed to get the JSON but I don't know how to echo the names for each ['follows']['user']['display_name']
This is the API:
https://api.twitch.tv/kraken/channels/kazutode/follows?limit=5&offset=0&client_id=nht9j8w0u4xazpm1fdc2fmkrqvoici
Here's my code so far (It's not alot, basically nothing xD)
<?php
$clientID = "nht9j8w0u4xazpm1fdc2fmkrqvoici";
$channel = "kazutode";
$limit = 5;
$offset = 0;
$response = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.$channel.'/follows?limit='.$limit.'&offset='.$offset.'&client_id='.$clientID), true);
?>
I know, I know. I should use curl but I don't know how so I stick to json_decode.
Can someone teach me how to get the wanted data out of the ['follows'] array?
You need a foreach loop. Here is some PHP+HTML mixed to give you an idea:
$table = '';
$table .= '<table>';
foreach($response['follows'] as $entry) {
$table .= '<tr>';
$table .= '<td>'.$entry['created_at'].'</td>';
$table .= '<td>'.$entry['user']['display_name'].'</td>';
$table .= '<td><a class="your-custom-class" href="'.$entry['_links']['self'].'">View channel</a></td>';
$table .= '</tr>';
}
$table .= '</table>';
echo $table;
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);
}
I'm pretty new to php and I can't figure out why 'testimonial_text' is not being wrapped within the 'testimonial-text' class. For some reason, it's outputting 3 elements and one of them is "testimonial text" but it's not within the "testimonial-text". "testimonial_author" is being correctly wrapped in "testimonial-author". Any ideas?
<?php
$rows = get_field('testimonials');
if($rows) {
foreach($rows as $row) {
$output = "<div class = 'testimonial-container'>";
$output .= "<p class = 'testimonial-text'>".$row['testimonial_text'] . "</p>";
$output .= "<p class = 'testimonial-author'>".$row['testimonial_author'] . "</p>";
$output .= "</div>";
echo $output;
}
}
?>
Following the image showing the contents of $rows in the comments it looks like the data you are returning has extra code and/or quotes in it. So I would recommend doing something like..
if($rows) {
$output = '';
foreach($rows as $row) {
$output .= "<div class = 'testimonial-container'>";
$output .= "<p class = 'testimonial-text'>" . strip_tags ($row['testimonial_text']) . "</p>";
$output .= "<p class = 'testimonial-author'>" . strip_tags ($row['testimonial_author']) . "</p>";
$output .= "</div>";
}
echo $output;
}
To remove any stray code that is getting output.
Worth noting #Naumov said to use strip_tags as well :)