php foreach() error message [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm creating a mobile website that takes feeds from a rss link and displays it as list..
here is my code
<div data-role="content">
<ul data-role="listview" data-theme="c" data-dividertheme="a" data-counttheme="a">
<?php
foreach($feed->query->results->item as $item) {
?>
<li>
<a href="article.php?notices=<?php echo $siteName;?>&origLink=<?php echo urlencode($item->guid->content);?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
</div>
but this is showing an error on line 12! which is below foreach($feed->query->results->item as $item)
EDIT:
Thanks everyone for your help. The rss link had slow response time so the YQL was timing out.. but now it's working..

Try to check for is_array like
if( is_array($feed->query->results->item) ) {
foreach($feed->query->results->item as $item) {
//The run the foreach loop
}
}
An if it is not an array then print the single result.Even we need to check whether they are getting results or not.

This is just guesswork but I think $feed->query->results->item is not an array. What you meant was $feed->query->results perhaps?
foreach ($feed->query->results as $item) {
// Do stuff
}

I think $feed->query->results->item this is empty. So it would be great if You provide the output of $feed.
then it will be easier to give You the exact result.

$feed->query->results->item is not an array ... or nothing is returning from the DB .. try to var_dump($feed->query->results->item) before the foreach and check if you are looping on the right value

Related

$_GET . Undefined Variable. Cant find solution [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having an issue and am out of ideas.
I'm trying to get a parameter from the URL, but PHP insists in saying that the variable is not defined.
The URL is
http://localhost/trendwatcher/index.php?date=2014-10-18
And the script is something like
<?php include "header.php"; ?>
<section id="stats">
Showing trends for <?php echo $GET_["date"]; ?>
</section>
And finally, the error:
Showing trends for
Notice: Undefined variable: GET_ in C:\xampp\htdocs\trendwatcher\index.php on line 4
Does anyone have any ideas?
Thanks!
Fix your code from:
<section id="stats">
Showing trends for <?php echo $GET_["date"]; ?>
</section>
to
<section id="stats">
Showing trends for <?php echo $_GET["date"]; ?>
</section>

executing foreach loop only once [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As we know foreach loop will execute till condition does not become false.
I want to execute it only once.
$i=0;
foreach($html->find('img') as $element)
{
if($i!=0)
break;
$logo= $element->src . '<br>';
$i++;
}
Is there any other solution for this? like foronce in place of foreach?
As we know foreach loop will execute till condition does not become false.
A foreach loop will execute once for every item of the container unless a return or break condition is defined within the foreach block.
I want to execute it only once.
Loops that executes once are called "don't use a loop in the first place". Here's your example fixed:
$logo = $html->find('img')[0]->src . '<br>';

PHP Variable Displaying - Not sure why? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Ok got the code below working but it keeps displaying the q value at the top of each page. what do I need to change to stop this happening. I can see the echo value is that what the problem is if so what should I change it too in order to prevent value displaying? Many thanks.
// capture referral url
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
parse_str( $referringPage['query'], $queryVars );
echo $queryVars['q']; // This is the search term used
}
// general form data insert
$sql="INSERT INTO refer_kws (kwid, keyword, kwdate)
VALUES('','".$queryVars['q']."',now())";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";
mysql_close($con)
remove this line:
echo $queryVars['q']; // This is the search term used
or turn it off by adding double slash at the beginning of the line like this:
// echo $queryVars['q']; // This is the search term used
First confirm that, do you want to display value or not. It seems that you have some misunderstanding regarding value insertion. you do not require to display value if you want to insert them in a database. You can remove whole line or make use of PHP comments 1. add // for one line comment 2. add /* at the starting of line and add */ at the end of line for multi line comments.

Getting a Parse error: syntax error, unexpected '}', expecting ',' or '; [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Here is my code. I'm new to php and just trying to edit a page using Kompozer. When I published the site back to the ftp server this error suddenly appeared on the page. Odd since I didn't even touch this code. (I have since touched the below code trying to fix it). Any help fixing this code would be much appreciated.
<?php
if(isset($_SESSION["pkg_error"]))
?>
<div class="error_msg_cont">
<?php
foreach($_SESSION["pkg_error"] as $error)
{
echo $error. "<br>"
}
?></div>
<?php
if(isset($_SESSION["msg"]))
{
echo '<div class="error_msg_cont">'. $_SESSION["msg"] .'<div>'
}
?>
On these two lines:
echo $error. "<br>" }
echo '<div class="error_msg_cont">'. $_SESSION["msg"] .'<div>' }
You need a semicolon before the closing }.
Closing a PHP code block (?>) implies a semicolon, but closing a block of code within a PHP code block (}) does not.
you missed ; on :
echo $error. "<br>" }
and
echo '<div class="error_msg_cont">'. $_SESSION["msg"] .'<div>' }
Actually, the error is self-explained.

How to display stored array from database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is how I'm storing the array into my database (part of a bigger code):
echo '<form action="" method="post">';
for ($count = 0; $count < $_GET["entries"]; $count++)
{
echo 'Enter a beginning to ending date for the week: <input type="text" name="week"><br/>';
}
echo '<input type="submit" name="submit"></form>';
It's within a tag, hence the echo.
I checked my database and I can see the first date, so it is being stored.
This is how I'm displaying my array (doesn't seem to work):
Where am I going wrong? Is it just the output or the input as well? I would really appreciate any suggestions for a possible answer. Thanks.
Current Specific Weeks: <?php
foreach ($currentWeeks as $currentWeeks)
{
echo "{$currentWeeks}\n";
}
Foreach mistake. You're using $currentWeeks for both the array and the element.
Use different variable name in foreach loop
foreach ($currentWeeks as $week)
^^^^^ // here
{
echo "{$week}\n";
}

Categories