I have the following code:
foreach ($model->details as $detail) {
if ($detail->Title) echo $detail->Title;
if ($detail->Info) echo '<br />'.$detail->Info;
}
Now I want to add something like this:
'<h2>Details</h2>'.'<hr />'
to the top of the output but only when the if's in the foreach clause are met. If neither of the if's is met I want nothing to display at all.
Can some help me tweak my code to accomplish this?
Like so?
foreach ($model->details as $detail) {
if ($detail->Title && $detail->Info) {
echo '<h2>Details</h2><hr />';
}
if ($detail->Title) echo $detail->Title;
if ($detail->Info) echo '<br />'.$detail->Info;
}
Addendum: I'd probably rather do something like this, to also avoid the uneccessary <br /> when there's no Title, but only Info
foreach ($model->details as $detail) {
$text = array();
if ($detail->Title) $text[] = $detail->Title;
if ($detail->Info) $text[] = $detail->Info;
if (!empty($text)) {
echo '<h2>Details</h2><hr />';
echo implode('<br />', $text);
}
}
You need to output to a string, or use the output buffering. The following example uses the string approach:
$sOutputHtml = NULL;
foreach ($model->details as $detail) {
if ($detail->Title) $sOutputHtml .= $detail->Title;
if ($detail->Info) $sOutputHtml .= '<br />'.$detail->Info;
}
if ($sOutputHtml !== NULL) {
echo '<h2>Details</h2>'.'<hr />' . $sOutputHtml;
}
Related
Why can not I make an echo inside a textarea after doing a foreach? the echo happens yes, but everything comes in fragments, one line in each text area.
Why does not everything come out in the same text area?
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
echo '<textarea>'.$strX.'</textarea>';
}
Concatenate the value and then echo into the textarea.
$value = null;
foreach ($array as $strX) {
$value .= 'myprefix'.$strX.PHP_EOL;
}
echo '<textarea>'.$value.'</textarea>';
https://3v4l.org/KZn2M
As I understood you so far. You just need to put the beginning tag of textarea before the for-loop. So your code would become:
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
echo '<textarea>';
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
}
echo '</textarea>';
Try this, put textarea outsside loop
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
echo '<textarea>';
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
}
echo '</textarea>';
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.
For a multiple choice quiz application i would like to show the dummy answers with the correct answer. But with the correct answer being in a different position at each different question.
This is what i've tried but it doesn't seem to be working:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$dummy_num = 1;
foreach ($query->result() as $row)
{
$rand_number = rand(1, 3);
if ($dummy_num == $rand_number)
{
$dummy_num = $rand_number + 2;
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
//Now echo the real answer
echo '<h4>Answer '.$rand_number.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>'; //Get id's for each.echo $row->id;
}
else
{
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
$dummy_num++;
}
}
echo '</div>';
echo ' <hr/>';
}
?>
You should use shuffle function.
In your case it will be:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$answers=$query->result();
shuffle($answers);
foreach ($answers as $nr=>$row)
{
echo '<h4>Answer '.($nr+1).'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
}
echo '</div>';
echo ' <hr/>';
}
?>
Put the answers in an array the use shuffle
$random_array = shuffle($answers);
All you need to do is put the keys for your answers in an array and call shuffle(). Something like this:
$keys = array_keys($answers);
shuffle($keys);
for ($key in $keys) {
echo $answers[$key];
}
I would suggest putting all the answers into an array and using the shuffle() function to randomize them. Once they're shuffled, just iterate the array with a loop and build the markup.
You could put the results into an array (1 correct and 3 incorrect), then shuffle, then output them?
$answers = array();
array_push($answers, "answer1");
array_push($answers, "answer2");
array_push($answers, "answer3");
array_push($answers, "answer4");
shuffle($answers);
foreach ($answers as $answer) {
echo $answer;
}
im getting this error:
Parse error: syntax error, unexpected T_ECHO in /home/labvc/public_html/AT/site/getimages.php on line 26
from this code:
<?php
echo '<br />';
echo '<div id=gallery>';
function getDirTree($dir,$p=true) {
$d = dir($dir);$x=array();
while (false !== ($r = $d->read())) {
if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
$x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
}
}
foreach ($x as $key => $value) {
if (is_dir($dir.$key."/")) {
$x[$key] = getDirTree($dir.$key."/",$p);
}
}
ksort($x);
return $x;
}
$tree = getDirTree("./res/gallery/painting/");
foreach($tree as $element => $eval) {
if (is_array($eval)) {
foreach($eval as $file => $value) {
if (strstr($file, "png")||strstr($file, "jpg")||strstr($file, "bmp")||strstr($file, "gif")) {
$item = $tree.'/'.$element.$file;
$itemthumb = $tree.'/thumbs/'.$element.$file;
echo '<img src="'.$itemthumb.'" alt="'.$file.'"/>';
}
}
}
}
echo '</div>';
echo '<br />';
echo 'tree: '.$tree.'<br />';
echo 'element: '.$element.'<br />';
echo 'file: '.$file.'<br />';
$abc="res/gallery/painting";
$def="01.png";
echo'<img src="'.$abc.'/thumbs/'.$def.'" alt="'.$def.'"/>';
echo '<br />';
line 26 is not an echo, theres not even an echo close to line 26
foreach($tree as $element => $eval) {
Any ideas?
I know it sounds silly, but are you actually looking at / editing the file you are debugging?
Any number of times it turned out I was in directory A/foo.c, when the code was being run out of directory B/foo.c. I always feel stooooopid after doing this.
Stick a print "foo!" in there to see if you are actually in the file you think you are.
This seems a suspicious line:
$item = $tree.'/'.$element.$file;
$tree should be an array, so if you get the error at runtime (as opposed to compile time) then it would make sense it would complain about this.