I got below code
<?php foreach ( $resultsb as $optionb ) : ?>
<option value="<?php echo $optionb->bID; ?>"><?php echo $optionb->book;?></option>
<?php endforeach; ?>
i know how the foreach is working here but i didn't get the : after the php statement what does the : do after the foreach statement
<?php foreach($resultsb as $optionb) : ?>
...
<?php endforeach; ?>
is nothing else then:
<?php foreach($resultsb as $optionb) { ?>
...
<?php } ?>
IMO it enhances the readability in files with a lot of html/xml etc.
Try like This
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of \$a: $v.\n";
}
Your code is true. You must check your array. Error not related to ":". It related to your $resultsb.
Related
trying to embed some html inside a do statement but PHPStorm flags it as a syntax error. Here's what I tried:
<?php do : ?>
<?php while ($time !== $foo); ?>
I had a look at the syntax in the documentation for php and couldn't see an example of it being done. Is there any way I can achieve it so it looks something like;
<?php do : ?>
<tr><td><p>some words</p></td></tr>
<?php while ($time !== $foo); ?>
Your do... while syntax is invalid.
From the PHP manual do-while page :
There is just one syntax for do-while loops:
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
Applied to your code, the syntax would be :
<?php do { ?>
<tr><td><p>some words</p></td></tr>
<?php } while ($time !== $foo); ?>
You can also use a simple while loop :
<?php while ($time !== $foo) { ?>
<tr><td><p>some words</p></td></tr>
<?php } ?>
Or
<?php while ($time !== $foo) : ?>
<tr><td><p>some words</p></td></tr>
<?php endwhile; ?>
you'd better "collect" everything and print it at once. Keep in mind to set the "stop the loop" condition sometime in the loop itself to avoid an infinite loop.
<?php
$html_out='';
do {
$html_out.='<tr><td><p>some words</p></td></tr>';
} while ($time !== $foo);
echo $html_out;
?>
There is no alternate syntax for do-while but you can do this
<?php while($time !== $foo): ?>
<tr><td><p>some words</p></td></tr>
<?php endwhile; ?>
Actually I work with PHP framework Codeigniter and I want to compare value from first foreach to second one, but I'm getting an error. Example here:
<?php foreach($posts->result() as $post): ?>
(html content)
<?php foreach($tags->result() as $tag) {
if($tag->id_users == $post->id_users) echo $tag->tag_name;
} ?>
(html content)
<?php endforeach; ?>
When I compare $post->id_users inner second foreach I'm getting error, how can I get around this?
Its better to avoid loop inside a loop
$tag_ids = array();
foreach($tags->result() as $tag) {
$tag_ids[] = $tag->id_users;
}
foreach ($posts->result() as $key => $post) {
if(in_array($post->id_users, $tag_ids)) {
}
}
You don't close the second foreach. For eg
<?php foreach($posts->result() as $post): ?> foreach1
(...some html)
<?php foreach($tags->result() as $tag) { if($tag->id_users == $post->id_users) echo $tag->tag_name; } ?> //foreach2
(...some html)
<?php endforeach; ?>
<?php endforeach; ?>
You should not use $posts->result() and $tags->result() inside foreach loop. Because it will go to check for every time while foreach is live.
Overall it decrease the performance of script.
<?php
$posts = $posts->result();
$tags = $tags->result();
foreach($posts as $post) {
?>
<< Other HTML code goes here >>
<?php
foreach($tags as $tag) {
if($tag->id_users == $post->id_users) {
echo $tag->tag_name;
}
?>
<< Other HTML code >>
<?php
}
}
I created 2 simple examples:
First example:
<?php $arr = array(1,2,3,4,5); ?>
<?php foreach ($arr as $element) ?>
<?php { ?>
<?php echo $element; ?>
<?php } ?>
output:
5 //Is this result wrong?
Second example:
<?php $arr = array(1,2,3,4,5); ?>
<?php foreach ($arr as $element) { ?>
<?php echo $element; ?>
<?php } ?>
output:
12345
What did I miss about the PHP syntax?
I know that there is an alternative foreach syntax, but in my opinion both shown examples should result in the same output. (Code tested with PHP version: 5.6.12)
Edit:
I know the tags are not needed in every line.
To be more precise: I want to know why the two examples give me 2 different results?
Based on the output, my guess is that:
<?php $arr = array(1,2,3,4,5); ?>
<?php foreach ($arr as $element) ?>
<?php { ?>
<?php echo $element; ?>
<?php } ?>
is being interpreted as:
<?php
$arr = array(1,2,3,4,5);
foreach ($arr as $element);
{
echo $element;
}
?>
Looks like a bug in the interpreter? See comments by Rizier123:
Not a bug: stackoverflow.com/q/29284075/3933332
The brackets after the foreach()/Do nothing here/; is just a statement-group: php.net/manual/en/control-structures.intro.php
Anyways, the code looks atrocious with the way you have written it. Please opt for cleaner code.
Reading through the comments under the question I think Jon Stirling explain this symptom the best:
Just guessing, but perhaps the ?> in the first example is actually being taken as the statement end (loops can be used without braces). At that point, the loop has happened and $element is the last value. Then the braces are just take as a code block which you echo, which is 5.
Over using <? php> is your problem.
You are completing the foreach context before outputting the result and not doing that in the second.
Look at your examples carefully and you should see what you are doing differently.
I don't know why you use so many php tags, but that's why it doesn't work!
try this:
<?php $arr = array(1,2,3,4,5);
foreach ($arr as $element)
{
echo $element;
} ?>
You don't need to use <?php and ?> every single line simply do:
<?php
$arr = array(1,2,3,4,5);
foreach ($arr as $element) {
echo $element;
}
?>
Or alternative syntax:
<?php
$arr = array(1,2,3,4,5);
foreach ($arr as $element)
{
echo $element;
}
?>
Or
<?php
$arr = array(1,2,3,4,5);
foreach ($arr as $element):
echo $element;
endforeach;
?>
When you are doing this:
<?php foreach ($arr as $element) ?>
<?php { ?>
<?php echo $element; ?>
<?php } ?>
PHP loops nothing because it sees
foreach ($arr as $element)
{
}
echo $element;
For some reason my code is an array one minute and not the next. I'm just learning php and I can't figure this out. I've used this page to figure most of this out: How to add two Google charts on the one page?
My code is also used on a page that shows the data as a table. On that page it works fine....but for some reason I'm getting Invalid argument supplied for foreach() on the pie charts page...and it's saying it's not an array. I'm not sure how to go about fixing this.
The url I'm using is ?action=scores&data=pie so it should list all of the universities in "get_universities()" (there's 3 there).
Can anyone help please?
The switch:
// Showing scores
case 'scores':
// Grab the Uni ID and use the appropriate query
if (isset($_GET['uni_id']))
{
$uni_id = $_GET['uni_id'];
$uni = get_university($uni_id);
}
else
{
$uni = get_universities();
}
// We have to display this data according to the request
if (isset($_GET['data']))
{
$data = $_GET['data'];
}
else
{
$data = "table";
}
// Display the data accordingly
include (root . '/view/' . $data . '_view.php');
break;
The pie graphs:
// Create the data table.
<?php foreach ($uni as $uni) : ?>
var data<?php echo $uni['university_id']; ?> = new google.visualization.DataTable();
data<?php echo $uni['university_id']; ?>.addColumn('string', 'Area');
data<?php echo $uni['university_id']; ?>.addColumn('number', 'Score');
data<?php echo $uni['university_id']; ?>.addRows([
['Teaching', <?php echo $uni['teaching_score']; ?>],
['Intl Outlook', <?php echo $uni['int_outlook_score']; ?>],
['Industry Income', <?php echo $uni['ind_income_score']; ?>],
['Research', <?php echo $uni['research_score']; ?>],
['Citations', <?php echo $uni['citations_score']; ?>]
]);
<?php endforeach ?>
// Set chart options
<?php foreach ($uni as $uni) : ?>
var options<?php echo $uni['university_id']; ?> = {'title':'<?php echo $uni['university_name']; ?> Scores',
'width':400,
'height':300};
<?php endforeach ?>
// Instantiate and draw our chart, passing in some options.
<?php foreach ($uni as $uni) : ?>
var chart<?php echo $uni['university_id']; ?> = new google.visualization.PieChart(document.getElementById('chart_div<?php echo $uni['university_id']; ?>'));
chart.draw(data<?php echo $uni['university_id']; ?>, options<?php echo $uni['university_id']; ?>);
<?php endforeach ?>
The table view (which works):
<?php foreach ($uni as $uni) : ?>
<td>
<a href="?uni=<?php echo $uni['university_id']; ?>">
<?php echo $uni['university_name']; ?>
</a>
</td>
<etc>
The problem is with your foreach statement:
foreach ($uni as $uni) :
Here you override the $uni variable. Use different names for the collection and item, eg:
foreach ($uni as $theUni) :
// also change instances of $uni below
EDIT
The above is wrong. The parsing of the first parameter of foreach happens only once so in that foreach it won't be a problem. However a foreach is not a new scope, so if you plan to reuse the array variable, you need to choose a different name as the iterator so it won't get overridden.
$a = array(); // $a is array
foreach ($a as $a) {
// $a is element of original $a
}
// $a is the last element of the original $a array
foreach ($a as $a) // Fail, since $a is not an array anymore
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can i group php foreach?i'v already tried 5 days,but still can't get it work
i don't know php foreach how it works, but I'm learning it,
Thanks to everyone's advice
original php:
<?php if(isset($this->leading) && count($this->leading)): ?>
<?php foreach($this->leading as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->primary) && count($this->primary)): ?>
<?php foreach($this->primary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->secondary) && count($this->secondary)): ?>
<?php foreach($this->secondary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
i tried
<?php if(isset($this->leading) && count($this->leading)) && (isset($this->primary) && count($this->primary)) && (isset($this->secondary) && count($this->secondary)): ?>
<!-- Leading items -->
<?php foreach (array($this->leading, $this->primary, $this->secondary) as $key=>$item) ($this->leading as $key=>$item): ?>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
but not work
As always, your assistance is appreciated!
Thanks!every one:)
Thanks,Steven!
do you mean something like this..?
<?php
$arr = array();
$arr[] = $this->leading;
$arr[] = $this->primary;
$arr[] = $this->secondary;
foreach($arr as $v) {
if(is_array($v) && (count($v) > 0)) {
foreach($v as $item) {
$this->item=$item;
echo $this->loadTemplate('item');
}
}
}
?>
Define a function like:
function foobar ($tmp) {
if(isset($tmp) && count($tmp)) {
foreach ($tmp as $key => $item) {
// Load category_item.php by default
$this->item = $item;
echo $this->loadTemplate('item');
}
}
}
and call it with your data:
foobar($this->leading);
foobar($this->primary);
foobar($this->secondary);
The simplest way to handle this is to first build a list containing all the items you need, and then use just one foreach to cycle through that list.
I'd also recommend some consistancy in whether the three variables (leading, primary, secondary) are defined. If you can guarantee that they're set and are arrays - even if they're empty arrays, it can make your code as simple as this:
<?php
$items = array_merge($this->leading, $this->primary, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>
If you really don't know whether the variables are set, and you can't refactor your code elsewhere to change this, you could do this instead:
<?php
$items = array();
if (isset($this->leading)) $items = array_merge($items, $this->leading);
if (isset($this->primary)) $items = array_merge($items, $this->primary);
if (isset($this->secondary)) $items = array_merge($items, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>