I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;
Related
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 :)
I'm trying to make a php slideshow and I'm almost done I just need to implement the next and back buttons which I thought were going to be easy, but apparently you can't increment indexes in php?
$sql = "SELECT pic_url FROM pic_info";
$result = $conn->query($sql);
$count = 0;
$dir = "http://dev2.matrix.msu.edu/~matrix.training/Holmberg_Dane/";
$source = "gallery.php";
if ($result->num_rows > 0) {
// output data of each row
$pic_array = array();
while ($row = $result->fetch_assoc()) {
$pic_array[$count] = $row['pic_url'];
$count++;
}
$index = 1;
echo "<img src= ' $dir$pic_array[$index]' />";
echo "<a href= '$dir$pic_array[$index + 1]'>next</a>";
echo "<a href= '$dir$pic_array[$index - 1]'>back</a>";
}
$conn->close();
?>
Try this
<?php
echo '<img src="'.$dir.$pic_array[$index].'">
next
back';
?>
I would suggest to place the url's in an array and loop over them.
$urls = ['website/url/for/image/image.jpg', 'another/url/image.jpg'];
foreach ($urls as $url) {
echo 'a href="http://'.$url.'"> Click </a>';
}
It is definitely more readable that way.
let's say if I have a txt file and inside has info sample like this:
amy,anderson,aldergrove,archery,anchovies,110
bill,bonds,burnaby,bowling,beer,100
cameron,carson,cameroon,cars,candy,120
henry,henderson,harrison,helping,hamburgers,90
dorothy,dust,denmark,driving,drinks,80
ed,edmunson,edmonton,eating,eggs,77
fred,fredrickson,fernie,flying,fries,140
and I want to use the file() and preg_split() function to call it out and show as a table what's the easiest way to do it?
I know how to call it out using file() function but I'm not sure how to replace the , and make it look like a table.
http://et4891.site90.com/sample.jpg <---this is a sample of how I want it to look like.
Below is what I did to call out the txt file.
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo "<tr>$one_persons_data</tr>";
}
echo "</table>"
?>
how should I modify this to make it look like the image I posted?
Thanks in adavance....
Is preg_split required? Better to use explode in this case. Anyway:
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo '<tr>';
$splitted = preg_split('/,/', $one_persons_data);
foreach ($splitted as $one) {
echo "<td>$one</td>";
}
echo '</tr>';
}
echo "</table>"
You can do this:
<?php
$rows = file('data.txt');
echo '<table>';
foreach($rows as $row){
echo '<tr>';
foreach(explode(',',$row) as $field){
echo '<td>';
echo htnlentities($field);
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
Hope this can help you.
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;
Good afternoon, everybody. I have a doubt.
I have a WHILE, and accurate list of the data within it FOREACH . Does anyone know how to do this?
I would be very grateful for the help.
Example code.
$UserIDS= array();
$query->execute();
while ($lista = $query->fetch()){
$mensagem = $lista['mensagem'];
$UserIDS[] = $lista['idUser'];
echo
'
//Data from Forech, was shown here.
<div class="avatar"></div>
<div class="text">
'.utf8_encode($mensagem).'
</div>
';
}
//FOREACH data has to list before the $message, if put into the forech WHILE not sure of the fact that I have an array inside.
foreach ($UserIDS as $idUsua) {
echo "<div class='avatar'>".box::avatar($idUsua)."</div>";
}
Anyone know how do I pull data from FOREACH and put inside the in WHILE? be very grateful for the help.
Referring to my earlier answer to your other question:
// fetch all the data in one go
$query->execute();
$data = $query->fetchAll();
// and then iterate it
foreach ($data as $lista) {
echo "<div id='avatar'>" . box::avatar($lista['idUser']) . "</div>";
echo "<div class='text'>" . utf8_encode($lista['mensagem']). "</div>";
}
Your existing box::avatar call is then still free (as before) to make separate PDO query calls.
why don't you take this part
foreach ($UserIDS as $idUsua) {
echo "<div class='avatar'>".box::avatar($idUsua)."</div>";
}
into a function like
function userId($UserIDS){
$userAvatarDivs="";
foreach ($UserIDS as $idUsua) {
$userAvatarDivs.= "<div class='avatar'>".box::avatar($idUsua)."</div>";
}
return $userAvatarDivs;
}
and call it on
while ($lista = $query->fetch()){
$mensagem = $lista['mensagem'];
$UserIDS[] = $lista['idUser'];
echo
'
//Data from Forech, was shown here.
<div class="avatar"></div>
<div class="text">
'.utf8_encode($mensagem).'
</div>
'.userId($UserIDS);
}