WP ACF echoes variable even if not wanted - php

I have strange problem, i've done the same thing before, many times.
But for some reason this one field just dosent want to co-operate whatsoever...
<?php if (have_rows('subfamilies_product_changes')) :
while ( have_rows('subfamilies_product_changes') ) : the_row();
$url = the_sub_field('subfamilies_product_changes_link');
echo "<div class='support-lahko'>";
echo "<div class='support-logo'>";
echo the_date( 'Y-m-d' );
echo "</div>";
echo "<div class='news-name-product'>";
echo "<h3 style='vertical-align: middle;'>" . the_sub_field('subfamilies_product_changes_title') ."</h3>";
echo "</div>";
echo "<div class='news-desc'>";
echo "</div>";
echo "</div>";
echo "<br>";
endwhile;
endif;
It seems, no matter where i put my <a> tags, this script is just going to go ahead and for some reason echo the link from $url = the_sub_field('subfamilies_product_changes_link'); into where ever it is defined. when i try to do something like <a href=' . $url . '> It will still just echo the url in <p> tags in seemingly random place.
Here is picture of what happens when i put $url inside href:
http://imgur.com/a/7AQsl
href stays empty, and wp just echoes url where it was defined. none of the text in image is a link.
I would just need to echo the url INTO the href, so i can iterate links into pdf files.
the_sub_field('subfamilies_product_changes_title')
produces text: Test Changes, as expected.

Short answer - use get_sub_field();
the_sub_field() outputs it immediately.
Glad that it helped - sometimes it is the smallest thing that ruins our day.

Related

My onclick events in my php include file do not work

I am working on my college web design course during my easter break and have run into a problem...
I have a menu on each page that I have removed from the html pages and written into a php file and that is used with an include in each page. Everything works fine apart from the onclick events that when clicked SHOULD load another page but at the moment does not do anything.
Here is my php file...
<?php
echo "<div id='leftPnl'>";
echo "<div class='navcon'>";
echo "<button class='static'
onclick='window.location.href='"."./index.html'".">Home</button>";
echo "<button class='accordion'>Gallery</button>";
echo "<div class='panel'>";
echo "<button class='static' onclick='window.location.href='"."./html/countryside.php'".">Countryside</button>";
echo "<div id='socialmedia'>";
echo "<img src='../images/sitewide/map.png'>";
echo "<br>Contact us on...<br>";
echo "<a class='fab fa-facebook-square fa-2x'
onclick='msg('"."Facebook"."')"."></a>";
echo "<a class='fab fa-instagram fa-2x' onclick='msg('"."Instagram"."')".">
</a>";
echo "<a class='fab fa-twitter-square fa-2x'
onclick='msg('"."Twitter"."')"."></a>";
echo "<a class='fab fa-tumblr-square fa-2x' onclick='msg('"."Tumblr"."')".">
</a>";
echo "<div id='clock'>";
echo "<p class='date'>{{ date }}</p>";
echo "<p class='time'>{{ time }}</p>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
on running the file and inspecting the element in Chrome, the url that the href uses is not formatted correctly the ./ are missing and a single quote is also missing.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Thanks.
PS, I apologise if this has already been asked - I looked and could not find the answer.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Yes.
Look at the output of the PHP. To take one example:
onclick='msg('Facebook')>
You start the attribute value with '.
Then you try to use ' inside it.
Then you forget the ' at the end!
There is no reason to nest any of this code inside PHP string literals. All that achieves it making it harder to debug.
Just write the HTML directly, outside of <?php ... ?> sections.
It wouldn't be a bad idea to get rid of the intrinsic event attributes too, and use JavaScript to bind event handlers.

PHP create Url in mysqli_query while loop for every record

I would like to create a link within a while loop where the anchor link is a record from the query and the href passes a variable from the same query to another page so I only have to create one page that displays information based on the passed variable.
echo "<td>";
echo ''$row['result']''';
echo "</td></tr>";
This link kills my page and return an error.
echo "<td>";
echo ''.$row['result'].'';
echo "</td></tr>";
Should fix your code your quotes are not closed right
Your href string concatenation is incorrect.
It seems like you tried closing your strings and concatenating them halfway through and then stopped. Even with Stack Overflow you can see the string errors.
This is how your href echo should look:
echo '<a href="stats_game.php?idGame=' .$row['idGame'] . '>' . $row['result'] . '</a>';
It works fine for me
echo "<td><a href='stats_game.php?idGame=".$row['idGame']."'>".$row['result']."</a></td>";

How to pass a query string value to a page loaded via AJAX?

I am trying to put a variable into an AJAX-loaded page link I can call it on the content it loads?
I have tried:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav={$array['username']}', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»{$array['username']}</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I tried putting the link inside and outside the php code but neither of them worked, I also tried a few differents ways of calling it including $array['username'] All I need is a way I can define the variable "nav" as username (which is set in a database) so I can use it on that content that is called by the link.
Is it even possible?
I am not sure I completely understood your question.
But perhaps you want to try this out:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav=<?php echo $array['username'];?>', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»", $array['username'], "</i>\n";
echo "</div>\n";
echo "</a><br />\n";

Echoing PHP within HTML

I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>

IMG SRC Using Path from MySQL and PHP

I have the following line of code which doesn't seem to be working:
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
The 'art1' is definitely returning '/imagename.png' so the URL should be complete however nothing being displayed.
Any ideas?
Thanks.
You really should be concatenating that string:
<?php
echo '<img src="/images/albumart'.$row['art1'].'"/>';
?>
and ideally break out of php to do this:
<img src="/images/albumart<?php echo $row['art1']; ?>"/>
If you're not familiar, the period in php joins strings. That's called concatenation. No need to echo little bits like that, in fact please never do it the way you demonstrate. The root of your issue is, as Geoff pointed out, you weren't closing the src param.
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
It looks like you are closing the img src paramter after the albumart part.,
try this:
echo "<img src='/images/albumart"; echo $row['art1']; echo "' />";

Categories