Variable inside echo <a> tag - php

How do I echo a variable inside the tag? this code does not seem to work. Any help is appreciated.
echo "<a href='productdetails.php?image="$results['image']"'><img src='images/".$results["image"]."' class='img-responsive' /></a><br />";

Like this
echo "<a href='productdetails.php?image="{$results['image']}"'>
<img src='images/{$results["image"]}' class='img-responsive' />
</a><br />";
Or
echo "<a href='productdetails.php?image=$results[image]'>
<img src='images/$results[image]' class='img-responsive' />
</a><br />";
Or like your original but with the correct concatenation
echo "<a href='productdetails.php?image=" . $results['image'] . "'>
<img src='images/".$results["image"]."' class='img-responsive' />
</a><br />";

It should be like this, you missed . before and after $results['image'], so proper concatenation is not happening:
echo "<a href='productdetails.php?image=" . $results['image'] . "'><img src='images/" . $results["image"] . "' class='img-responsive' /></a><br />";
Method 2:
Take it in a variable:
$image = $results['image'];
echo "<a href='productdetails.php?image=" . $image . "'><img src='images/" . $image . "' class='img-responsive' /></a><br />";

Or like this:
echo
'<a href="productdetails.php?image=' . $results['image'] . '">
<img src="images/' . $results['image'] . '" class="img-responsive" />
</a><br />';

Related

Reverse read xml file in php

This is my php code
$xml=simplexml_load_file("data/listings.xml") or die("Error: Cannot create object");
foreach($xml->children() as $listings){
echo "<div class='col-md-4 top-text'>";
echo "<a href='?p=single&n=" . $listings->time . "'><img src='thumbnails/" . $listings->images . ".jpg' class='img-responsive' alt=''></a>";
echo "<h5 class='top'><a href='?p=single&n=" . $listings->time . "'>" . $listings->title . "</a></h5>";
echo "</div>";
}
I can not read from end to start using the below code.
foreach(array_reverse($xml->children()) as $listings){
echo "<div class='col-md-4 top-text'>";
echo "<a href='?p=single&n=" . $listings->time . "'><img src='thumbnails/" . $listings->images . ".jpg' class='img-responsive' alt=''></a>";
echo "<h5 class='top'><a href='?p=single&n=" . $listings->time . "'>" . $listings->title . "</a></h5>";
echo "</div>";
}
When you call children() on a SimpleXMLElement, you get another SimpleXMLElement back and not an array, so you can't call array_reverse() on it. A simple way round this is to use a for() loop instead...
for ( $i=$xml->children()->count()-1; $i>=0; $i-- ) {
$list = $xml->children()[$i];
echo "<div class='col-md-4 top-text'>";
echo "<a href='?p=single&n=" . $listings->time . "'><img src='thumbnails/" . $listings->images . ".jpg' class='img-responsive' alt=''></a>";
echo "<h5 class='top'><a href='?p=single&n=" . $listings->time . "'>" . $listings->title . "</a></h5>";
echo "</div>";
}
The reason why you can use it in a foreach() normally is that it implements Traversable, which allows you to process it using various methods as though it was an array.

html code inside php if statement

I am trying to add a video tag if the PHP code receives it from a $_GET method.
The problem is in the 2nd echo
<?php
if($_GET['video1'] == NULL) {
}
} else {
$vid = $_GET["video1"];
echo '<video width="320" height="240" controls>';
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
echo '</video>';
}
?>
PHP is not recursively embeddable:
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
You are ALREADY in "php mode" with your echo statement.Therefore you cannot "go deeper" into php mode.
Why do you need such a hideously ugly convoluted statement anyways? Why can't you simply have
echo '<source src=' . $_GET["video1"] . ' type="video/mp4">';
?
Replace:
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
With:
echo '<source src=' . $_GET["video1"] . ' type="video/mp4">';
There is no need to nest PHP code clocks.

Add item to different div from PHP generated List (w/jQuery + AJAX)

I have little experience with AJAX and jQuery so apologize if this seems trivial.
I have a list of ingredients that are generated from a DB via PHP foreach loop:
<div class="pure-u-1 pure-u-md-2-5 pure-u-lg-2-5 content-left">
<div id="scroll" class="card">
<h2 class="is-center">green</h2>
<ul id="greens" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($greens as $green) {
echo "<li>";
echo "<span class='item-name-small'>" . $green['name'] . "</span>";
echo "<span class='item-description-menu'>" . $green['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $green['id'] . "' name='" . $green['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}?>
</ul>
<h2 class="is-center">essentials</h2>
<ul id="essentials" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "veggies") || ($essential['subtype'] == "fruit")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
<h2 class="is-center">crunch</h2>
<ul id="crunch" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "crunch")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
<h2 class="is-center">grains</h2>
<ul id="grains" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "grains")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
</div> <!-- END CARD -->
</div> <!-- END ESSENTIALS -->
Currently, I have it that next to each ingredient, a - and + appear next to a text input. What I'm trying to do is change the code that when a customer clicks on each individual item, it is added to a different div that has a summary of their order. The second div is on the same page as the item list.
If you just want to copy the list item to a different div you can do it like this:
$(function (){
$('ul.card-content-ingredients li').click(function(){
$('#new-div-id ul').append($(this));
}
});

PHP code to display db data

Hi guys I have a problem with my PHP, What I am trying to achieve is this:
<li>Product name from DB
<strong>£499<img src="images/thumb.png" alt="Product name from DB"/></strong>
</li>
and here is my code with php:
<ul id="items">
<?php while($product_data = mysql_fetch_array($query_product_result))
{
$num_rows_products = $num_rows_products - 1;
Print "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . " <strong> Name: " . $product_data["title"] . "</strong></a>";
Print "<strong>Price: £" . $product_data["price"] . "'><img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
if($num_rows_products > 0)
Print '<p> nu products ? wtf</p>';
}
?>
For some reason all I get is an image with a hyperlink, no title and no price, any chance you can spot the mistake?
Your not closing the <a> tag properly. See the code below. Notice the missing ">" in your code?
Print "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . "'> <strong> Name: " . $product_data["title"] . "</strong></a>";
Print "<strong>Price: £" . $product_data["price"] . "<img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
Use Echo instead. Print won't displays concatenated string.
<ul id="items">
<?php while($product_data = mysql_fetch_array($query_product_result))
{
$num_rows_products = $num_rows_products - 1;
echo "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . " <strong> Name: " . $product_data["title"] . "</strong></a>";
echo "<strong>Price: £" . $product_data["price"] . "'><img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
if($num_rows_products > 0)
echo '<p> nu products ? wtf</p>';
}
?>

php pass parameter issue

This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>

Categories