Error with php switch function - php

I´ve got the following code and when I paste the code to my page, I get a white page, can you guys help me?
PHP
<?php $strRendersettings = ($this->settings)? 'settings' : 'view'; ?><?php if (count($this->data)): ?>
<ul>
<?php
switch($_SERVER['HTTP_HOST'])
{
case ("www.domain.de"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
case ("www.domain.com"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
case ("www.domain.fr"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
}
?>
<?php endforeach; ?>
</ul>
<?php else: ?>
<?php endif; ?>
I need the code because of some different top level domains.
Thanks :)

The porblem is that you never end you foreach loops.
You should end each foreach: loop with endforeach;. Now you have 3 loops and only one closing endforeach;
For example this code works without a problem:
<?php
$array = [1=>2, 3=> 4];
$array2 = [5=>6,7=>8];
foreach ($array as $k => $v):
foreach ($array2 as $i => $j):
echo $i.' '.$j."<br />";
endforeach;
endforeach;
while the following doesn't:
<?php
$array = [1=>2, 3=> 4];
$array2 = [5=>6,7=>8];
foreach ($array as $k => $v):
foreach ($array2 as $i => $j):
echo $i.' '.$j."<br />";
//endforeach;
endforeach;
I advice you also to add at the beginning of your PHP file:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
to have all warnings and error displayed. Of course on production you should disable error reporting

It looks like you're breaking the foreach loop with the break of the switch. You should end the foreach inside each case of the switch.

See you PHP error log for error messages. When there is a parse error, the page is shown blank, and the error message goes to the PHP error log.
In your code, <?php endforeach; ?> appears to be outside the switch. I guess each foreach should have its own endforeach before the switch's break.

Correct me if I'm wrong, but you are doing the exact same thing for each of the three scenarios. I would alter my code to this:
<?php $strRendersettings = ($this->settings)? 'settings' : 'view'; ?>
<?php if (count($this->data)): ?>
<ul>
<?php
switch($_SERVER['HTTP_HOST'])
{
case "www.domain.de":
case "www.domain.com":
case "www.domain.fr":
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home">
<a href="">
<strong>
<?php echo $arrItem['text']['new_docs_titel']; ?>:
</strong><br>
<p>
<?php echo $arrItem['text']['new_docs_Text']; ?>
</p>
</a>
</li>
<?php
endforeach;
break;
}?>
</ul>

You need to add endforeach statement before every break statement. Add that and your error will get sloved.
Hope this help :)

Related

Display MySQL request in PHP

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Looping through a PHP array

I have the following PHP array structure:
$set = array();
$set[] = array('firstname'=>'firstname 1',
'lastname'=>'lastname 1',
"bio"=>array('paragraph 1 of the bio, 'paragraph 2 of the bio','paragraph 3 of the bio'),
);
I then access the array with the following:
<?php $counter = 0;
while ($counter < 1) : //1 for now
$item = $set[$counter]?>
<h1><?php echo $item['firstname'] ?></h1>
<h1><?php echo $item['lastname'] ?></h1>
<?php endwhile; ?>
I'm uncertain how I can loop through the "bio" part of the array and echo each paragraph.
So as a final output, I should have two h1s (first and last name) and three paragraphs (the bio).
How can I go about doing this?
Use foreach loop
foreach($item['bio'] as $listitem) {
echo $listitem;
}
You don't need to use a manual counter, you can use foreach. It's generally the easiest way and prevents off-by-one errors.
Then you need a second inner loop to loop through the bio.
<?php foreach ($set as $item): ?>
<h1><?php echo $item['firstname'] ?></h1>
<h1><?php echo $item['lastname'] ?></h1>
<?php foreach ($item['bio'] as $bio): ?>
<p><?php echo $bio; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
On a related note; you probably want to look into escaping your output.
Add into the while loop also this:
<?php foreach ($item['bio'] as $paragraph): ?>
<p><?php echo $paragraph; ?></p>
<?php endforeach; ?>
Note that used coding style is not optimal.
Try:
foreach($set as $listitem) {
if(is_array($listitem)) {
foreach($listitem as $v) //as $set['bio'] is an array
echo '<h1>' .$v . '</h1>';
} else
echo '<p>'.$listitem.'</p>';
}

Unable to apply foreach loop properly in PHP

I want two posts at each slide. but getting only one slide. I am new in programming, please help me.
$widget_id = $widget->id.'-'.uniqid();
$settings = $widget->settings;
$navigation = array();
$captions = array();
$i = 0;
?>
<div id="slideshow-<?php echo $widget_id; ?>" class="wk-slideshow wk-slideshow-revista-articles" data-widgetkit="slideshow" data-options='<?php echo json_encode($settings); ?>'>
<div>
<ul class="slides">
<?php foreach ($widget->items as $key => $item) : ?>
<?php
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php $i=$i+1;?>
<?php endforeach; ?>
</ul>
<?php if ($settings['buttons']): ?><div class="next"></div><div class="prev"></div><?php endif; ?>
<?php echo ($settings['navigation'] && count($navigation)) ? '<ul class="nav">'.implode('', $navigation).'</ul>' : '';?>
<div class="caption"></div><ul class="captions"><?php echo implode('', $captions);?></ul>
</div>
</div>
http://i.stack.imgur.com/sy1ih.png
you're missing the { after the foreach and at the end of the loop.
<?php foreach ($widget->items as $key => $item) {
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php
$i=$i+1;
}
?>
Your foreach syntax looks fine. That syntax is a lot easier for some people to read when embedded in HTML than the traditional braces.
Can I ask what the $this variable refers to? I don't see this instantiated anywhere in your code? Is it supposed to be $item instead?
$settings['index'] never changes within the loop, however $i does.
Change to: ($i<2)

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

endforeach in loops?

I use brackets when using foreach loops. What is endforeach for?
It's mainly so you can make start and end statements clearer when creating HTML in loops:
<table>
<? while ($record = mysql_fetch_assoc($rs)): ?>
<? if (!$record['deleted']): ?>
<tr>
<? foreach ($display_fields as $field): ?>
<td><?= $record[$field] ?></td>
<? endforeach; ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
</td>
</tr>
<? else: ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? endif; ?>
<? endwhile; ?>
</table>
versus
<table>
<? while ($record = mysql_fetch_assoc($rs)) { ?>
<? if (!$record['deleted']) { ?>
<tr>
<? foreach ($display_fields as $field) { ?>
<td><?= $record[$field] ?></td>
<? } ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action) { ?>
<option value="<?= $action ?>"><?= action ?>
<? } ?>
</td>
</tr>
<? } else { ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? } ?>
<? } ?>
</table>
Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax (endforeach) form can make things easier for your brain to parse. With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing.
It's the end statement for the alternative syntax:
foreach ($foo as $bar) :
...
endforeach;
Useful to make code more readable if you're breaking out of PHP:
<?php foreach ($foo as $bar) : ?>
<div ...>
...
</div>
<?php endforeach; ?>
as an alternative syntax you can write foreach loops like so
foreach($arr as $item):
//do stuff
endforeach;
This type of syntax is typically used when php is being used as a templating language as such
<?php foreach($arr as $item):?>
<!--do stuff -->
<?php endforeach; ?>
It's just a different syntax. Instead of
foreach ($a as $v) {
# ...
}
You could write this:
foreach ($a as $v):
# ...
endforeach;
They will function exactly the same; it's just a matter of style. (Personally I have never seen anyone use the second form.)
How about this?
<ul>
<?php while ($items = array_pop($lists)) { ?>
<ul>
<?php foreach ($items as $item) { ?>
<li><?= $item ?></li>
<?php
}//foreach
}//while ?>
We can still use the more widely-used braces and, at the same time, increase readability.
Using foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs
So for big apps, receiving many users this would be the best solution
How about that?
<?php
while($items = array_pop($lists)){
echo "<ul>";
foreach($items as $item){
echo "<li>$item</li>";
}
echo "</ul>";
}
?>

Categories