Im getting Parse error from:
$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
How can I improve this code to avoid Parse error? I understand that I can't use IF statement in $var
When I Echo Variable's its works like a charm and I get the results that I want. But how can I assign the result of Echo Variable's (on Line:10) to $uphalfh
include_once './wp-config.php';
include_once './wp-load.php';
include_once './wp-includes/wp-db.php';
// A name attribute on a <td>
$price30in = $xpath->query('//td[#class=""]')->item( 1);
$price30out = $xpath->query('//td[#class=""]')->item( 2);
// Echo Variable's
if(isset($price30in)) { echo $price30in->textContent;} if(isset($price30out)) { echo ", " . $price30out->textContent; }
// The wrong CODE i tried:
// $uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
// if(isset($price30out)) { echo ", " . $price30out->textContent; }
// Create post object
$my_post = array();
$my_post['post_title'] = 'MyTitle';
$my_post['post_content'] = 'MyDesciprion';
$my_post['post_status'] = 'draft';
$my_post['post_author'] = 1;
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
update_post_meta($post_id,'30_min',$uphalfh);
$uphalfh = isset($price30in);
if(isset($price30in)) {
echo $price30in->textContent;
}
if(isset($price30out)) {
echo ", " . $price30out->textContent;
}
remove the assignment.
if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
// $uphalfh = you have to complete the variable initialising here and then continue with the conditional statement or remove the variable initialising and continue with the conditional statement because $var = if(conditon){ }else{ }; is wrong syntax in php
if(isset($price30in)) {
echo $price30in->textContent;
}
if(isset($price30out)) {
echo ", " . $price30out->textContent;
}
echo $uphalf = (isset($price30in)) ? $price30in->textContent : "";
The if clause does not return a value, which means you can't use it in the context of assigning a value to variable because there is no value to be had.
$uphalfh = if(isset($price30in)) {
echo $price30in->textContent;
} if(isset($price30out)) {
echo ", " . $price30out->textContent;
}
The above code will not work as you expected it, the bellow code should work as expected -- the value of $uphalf will be set to either $price30in->textContent or $price30out->textContent.
if(isset($price30in)) {
$uphalfh = $price30in->textContent;
} if(isset($price30out)) {
$uphalfh = ", " . $price30out->textContent;
}
Only if you also want this result to be outputted to the browser (directly visible to a visitor) you could use echo.
if(isset($price30in)) {
$uphalfh = $price30in->textContent;
}
if(isset($price30out)) {
$uphalfh = ", " . $price30out->textContent;
}
echo $uphalfh;
A simple edit did the trick! That solved my problem :-)
I replaced:
$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
With this:
$uphalfh = $price30in->textContent. ', ' . $price30out->textContent;
Related
I have this function:
function get_content($text_to_match) {
$query = "SELECT * ";
$query .= "FROM table_name ";
$query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
$cont = mysqli_query($connection, $query);
if($content = mysqli_fetch_assoc($cont)) {
return $content;
} else {
return null;
}
}
But when I call it like:
<div>
<?php
for ($i = 1; $i < count(get_content("text_to_match")); $i++) {
echo '<article>' .
'<h3>' . get_content("text_to_match")["string1"] . '</h3>'.
'<p>' . get_content("text_to_match")["string2"] . '</p>' .
'</article>';
}
?>
</div>
I only get the first match in the DB repeated as many times as the number of found items.
Where have I gone wrong?
use this code then fetch data properly
while($content = mysql_fetch_array($cont))
{
return $content;
}
Your logic is at fault. You are calling get_content function to get all matches for the loop, as well as to get individual elements out of the list. This is:
bad logic - the 2nd use case doesn't make sense
excessive - you shouldn't need to run a database query just to output an already retrieved result
What you probably want to do is:
foreach (get_content('text_to_match') as $content) {
echo '<article>';
echo '<h3>' . $content['string1'] . '</h3>';
echo '<p>' . $content['string2'] . '</p>';
echo '</article>';
}
With a few modifications in combination with tips from #Anant and #Unix One's answer, I arrived at this working solution:
Function definition
function get_content($text_to_match, $multiple=false) {
$query = "SELECT * ";
$query .= "FROM table_name ";
$query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
$cont = mysqli_query($connection, $query);
if ($multiple) {
$content_array = [];
while($content = mysqli_fetch_array($cont)) {
$content_array[] = $content;
}
return $content_array;
} else {
if($content = mysqli_fetch_assoc($cont)) {
return $content;
} else {
return null;
}
}
}
Function calls
<?php
/* multiple items */
foreach(get_content("text_to_match", true) as $content) {
echo '<article>' .
'<h3>' . $content["string1"] . '</h3>' .
'<p>' . $content["string2"] . '</p>' .
'</article>';
}
?>
<?php
/* one item */
echo get_content("text_to_match")["string"];
?>
Here's my piece of code(full body code):
<body>
<script type='text/javascript'>
function AddEvent(Syear, Smonth, Sday, Eyear, Emonth, Eday, hallNumber){
...
}
</script>
<?php
function GetMonthByCoding($first , $second , $third) {
...
}
function GetDateByCoding($coding){
...
}
function GetDateFromLine($line){
...
}
$userid = '...';
$magicCookie = 'cookie';
$feedURL = "...";
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
if ($title == "HALL") {
$summary = stripslashes($entry->summary);
$date = GetDateFromLine($summary);
echo ("<script type='text/javascript' language='JavaScript'> AddEvent(" . $date['start']['year'] . ", " . $date['start']['month'] . ", " . $date['start']['day'] . ", " . $date['end']['year'] . ", " . $date['end']['month'] . ", " . $date['end']['day'] . "); </script>");
}
}
?>
</body>
AddEvent() is JavaScript function defined earlier.
What I get in my browser is:
entry as $entry) { $title = stripslashes($entry->title); if ($title == "HALL") { $summary = stripslashes($entry->summary); $date = GetDateFromLine($summary); echo (""); } } ?>
Looks like it was an echo but as you can see there is no echo right in the middle of foreach.
Can anyone say what I am doing wrong?
PHP is not installed, or it is not enabled, or the file is not a .php file or the server has not been told to recognise it as a file to parse.
Try View Source and you should see all your PHP code. The only reason part of it shows up is because everything from <?php to the first > is considered by the browser to be an invalid tag.
I found the problem, it was in the name of variable sxml. I renamed it and the problem escaped.
I have the following code with the if...else statement within a while loop.
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$nextcolr = next($colour);
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
I can't work out why what ever is in the else statement isn't being executed, even if I switch the two statements and reverse the operator. Could anyone help me?
The entire while loop:
while($row = mysql_fetch_array($result))
{
echo "by <a href='/neuro/profile.php?userid=$row[MemberID]'>" . $row['FirstName'] . " " . $row['LastName'] . "</a> on " . $row['Timestamp'] . " | " . $row['NumberOfComments'] . " comments.";
echo "<div id='blog' style='background-color:#";
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
echo "'><a href='blog.php?threadid=" . $row['tID'] . "'>" . $row['Title'] . "</a></div>";
}
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
while ... {
$nextcolr = next($colour);
if ($nextcolr === FALSE)
{
reset($colour);
}
echo current($colour);
}
is how your while loop should look like. If I am right, you are also defining $colour in the while loop, which might cause problems.
If all this is in the while loop, then you are re-declaring the array on each iteration, thus returning the array internal pointer to the beginning with each iteration.
If you want to iterate this array multiple times, you could do it this way:
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$i = 0;
while ... {
...
echo $colour[$i++ % count($colour)];
...
}
So you don't need this if-else block.
The problem with your original while loop is that you never change the value of $nextcolr.
Thus, it always remains FALSE and the else part never gets executed.
I am trying to process a form that is dynamically created and therefore varies in length. The while loop seems to work fine. However, the 'if' statement is not; it should only print the startId$i and corId$i if and only if the form's particular text field was filled in. The code is printing a line for every text field on the form, regardless of if it was left empty or not.
$i = 0;
while(!is_null($_POST["startId$i"])){
if(($_POST["startId$i"]) != ""){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
$i = 0;
while(isset($_POST["startId$i"])){
if( !empty($_POST["startId$i"]) ){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
Can you manage with fields names ?
If yes, better way is to name inputs with name="startId[0]" and name="corId[0]" and so on...
Then in PHP you just do:
$startIds = $_POST['startId'];
$corIds = $_POST['corId'];
foreach ( $startIds as $k => $startId ) {
if ( !empty($startId) ) {
$corId = $corIds[$k];
echo "startId: " . $startId . " ---<br>";
echo "corId: " . $corId . " ---<br>";
}
}
You should use empty() in this case:
if(!empty($_POST["startId$i"])) {
...
}
I suggest to check the real content of $_POST. You can do that via var_dump($_POST);
You may find out, for example, that the empty fields contain whitespaces. In that case the trim() function may help.
For example:
while(isset($_POST["startId$i"])){
if(trim($_POST["startId$i"])){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
I am trying to get the names of my friends using FB Graph API with this call :
$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);
echo "Friends : $friends\n";
This gives me a list of the form :
{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}
I want to be able to store only the NAMES in an array. How do I use $friends to get the names ? Something like $friends['name'] doesn't seem to work.
Please help.
Thank you.
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
echo $friend['name'];
}
The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/
If this doesn't work try:
$friends = json_decode($friends);
foreach($friends->data as $friend)
{
echo $friend->name;
}
Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object
$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
echo "MESSAGE: ", $post->message, "<br>";
echo "NAME: ", $post->name, "<br>";
echo "TYPE: ", $post->type, "<br>";
echo "ID: ", $post->id, "<br>";
echo "LINK: ", $post->link, "<br>";
echo "PERMALINK: ", $post->permalink_url, "<br>";
echo "CREATED: ", $post->created_time, "<br>";
if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }
echo "SHARES: ", $shares, "<br>";
if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }
echo "REACTIONS: ", $reactions, "<br>";
if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }
echo "COMMENTS: ", $comments, "<br>";
if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }
echo "LIKES: ", $likes, "<br>";
echo "<br><br>";
}