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>';
}
?>
Related
hello guys i am trying to make a foreach loop in a while loop. in foreach loop i try to produce list elements with divs inside them but i have very strange output
here is my code
$countercat= 0;
$classvar= 1;
echo "<div class='selector-page'>";
echo "<div class='selector'>";
echo "<ul>";
while ($countercat <= 8){
$stmt=$conn->prepare('SELECT eidos, name, meta_keys, address, telephone, perioxi, st_img, st_open, st_close, lat, longtit FROM magazia WHERE perioxi= :perioxi AND eidos= :eidos ORDER BY st_id ASC');
$stmt->bindParam(':perioxi', $name, PDO::PARAM_STR);
$stmt->bindParam(':eidos', $eidos, PDO::PARAM_STR);
$eidos= $c_titles[$countercat]['c_name'];
$stmt->execute();
$allrows=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($allrows as $row) {
echo "<li>";
echo "<div class='p". $classvar . " w3-card targetDiv w3-margin-top cardsmar'>";
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'";
echo "<div class='w3-container w3-center'>";
echo "<h3>" . $row['name'] ."</h3>";
echo "<p>" . $row['eidos'] . "</p>";
echo "<p>" . $row['address'] . " , " . $row['perioxi'] . "</p>";
echo "<p>" . $lang['wrlt'] . " : " . $row['st_open'] . "-" . $row['st_close'] . "</p>";
echo "<a href='katastimata.php?name=" . $row['name'] . "' role='button' class='w3-button w3-round w3black'>" . $lang['t9'] . "</a><br />";
echo "<a href='https://www.google.com/maps?q=loc:" . $row['lat'] . "," . $row['longtit'] . "' role='button' class='w3-button w3-round w3-green btnmar'>" . $lang['spot2'] . "</a>";
echo "</div>";
echo "</div>";
echo "</li>";
}
$countercat++;
$classvar++;
}
echo "</ul>";
echo "</div>";
echo "</div>";
}
?>
here is an image from my debugger consonle
as you see in the image inside in the ul tag exists only one li elemenemt
and the rest of them are out side ul /ul.
my first thought was that is not valid to put div tag in a li tag but this is not true if i use this in the top of my file
DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/html1-transitional.dtd"
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang:"en"
i am stack here for so long
what am i missing guys?
thanks in advance
vaggelis
You Didn't Close the <img> tag here.
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'";
You Must Close the tag as
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'/>";
I have written this code, sorry I'm pretty new to PHP.
I have a loop which grabs all the horses from the database.
I have created a sell button underneath each entry, the problem I have is that when I hit "Sell" on a single entry it "Sell"s ALL of the entries.
Here is my code;
$query = "SELECT * FROM horses WHERE (owner = '$id') AND (status = 'active')";
if($result = $connect->query($query)) {
echo "<ul id='horses' class='row'>";
while ($row = $result->fetch_assoc()) {
echo "<li class='col-sm-4'>";
echo "<div class='horse-wrap'>";
echo "<h2>" . $row['name'] . "</h2>";
echo "<div class='details'>";
echo "<strong>Age:</strong> " . $row['age'] . "<br>";
echo "<strong>Colour:</strong> " . $row['colour'] . "<br>";
echo "<strong>Country:</strong> " . $row['country'] . "<br>";
echo "<strong>Value:</strong> £" . number_format($row['value']) . "<br>";
echo "</div>";
echo "<h3>Record</h3>";
echo "<ul class='history row'>";
echo "<li class='col-sm-3'>Runs: " . $row['runs'] . "</li>";
echo "<li class='col-sm-3'>Wins: " . $row['first'] . "</li>";
echo "<li class='col-sm-3'>2nd: " . $row['second'] . "</li>";
echo "<li class='col-sm-3'>3rd: " . $row['third'] . "</li>";
echo "</ul>";
echo "<ul class='skills'>";
echo "<h4>Speed</h4><li><span class='skill' style='width: " . $row['skill_speed'] . "%;'>" . $row['skill_speed'] . "</span></li>";
echo "<h4>Jumping</h4><li><span class='skill' style='width: " . $row['skill_jump'] . "%;'>" . $row['skill_jump'] . "</span></li>";
echo "<h4>Temperament</h4><li><span class='skill' style='width: " . $row['skill_temperment'] . "%;'>" . $row['skill_temperment'] . "</span></li>";
echo "<h4>Endurance</h4><li><span class='skill' style='width: " . $row['skill_endurance'] . "%;'>" . $row['skill_endurance'] . "</span></li>";
echo "</ul>";
// SELL HORSE BUTTON
echo "
<form action='./stables.php' method='post'>
<input type='hidden' value='" . $row['value'] . "' name='sellvalue' />
<input type='hidden' value='" . $row['name'] . "' name='sellname' />
<input type='submit' value='Sell Horse' name='sell' class='sell' />
</form>
";
echo "</div>";
echo "</li>";
// SELL HORSE
if(!empty($_POST["sell"])) {
//REMOVE HORSE FROM USER
$horseName = $row['name'];
$status = 'sold';
if($updateHorse = $connect->prepare("UPDATE horses SET status = ? WHERE name = ?")) {
$updateHorse->bind_param('ss', $status, $horseName);
$updateHorse->execute();
$updateHorse->close();
}
//ADD MONEY TO USER
$updateAmount = $money + $row['value'];
if($updateUser = $connect->prepare("UPDATE users SET money = ? WHERE id = ?")) {
$updateUser->bind_param('ss', $updateAmount, $id);
$updateUser->execute();
$updateUser->close();
}
}
}
echo "</ul>";
} else {
echo "You currently have no horses.";
}
You're doing the update inside of a loop, but you're never checking to see if the loop's current horse is the one the customer is trying to sell. As it's written, it is going to execute for every one.
You're checking to see if the form was submitted:
// SELL HORSE
if(!empty($_POST["sell"])) {
This is the perfect place to also make sure you have the right horse:
// SELL HORSE
if(!empty($_POST["sell"]) && $row["name"] === $_POST["sellname"]) {
I'm trying to do a condition within an if statement.
I have a current if statement which determines odd and even for css styling.
Within this I want to add another if statement for the reference number. (outlined as Ref in the code).
I'm not sure how to go about this but I want to add something along the lines of
if ref == 1 then echo ref, I've tried putting an if within an if and I just keep getting syntax errors. Can someone point me in the right direction please?
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
the bit I tried to do with 2nd if statement:
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates1 ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
$sch = $row1['schuedled'];
else if ($sch == '1') {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " .$row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
else {
echo "<blockquote class='example-right'>" . $row1['update'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='assyst-ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
Try something like this:
while($row1 = mysql_fetch_array($result1)){
$html = '';
if (++$x % 2 ){
$html .= "<blockquote class='example-right'>";
} else {
$html .= "<blockquote class='example-obtuse'>";
}
$html .= $row1['update'];
if($row1['schuedled'] == '1'){
$html .= "<div class='ref'>Ref: " .$row1['ref'] . "</div>";
}
$html .= "more html";
$html .= "more html";
$html .= "more html";
$html .= "</blockquote>";
echo $html;
}
I am trying to perform several line break (br) in my table cell.
But I have problem with the syntax.
Below is my code to perform line break (br) within a single cell, but it doesn't work.
How should I go about doing it?
echo("<tr><td>".$row[0].
<br/>
$row[1].
<br/> $row[2].
<br/>
"<img src=images/".$row["skuCode"]."_1.jpg>".
<br/>
View".
"</td></tr>");
You're not using quotes properly, but I think it would be much easier to use a heredoc.
echo <<<HTML
<tr><td>$row[0]<br/>$row[1]<br/>$row[2]<br/>
<img src=images/$row[skuCode]_1.jpg<br/>
<a href=viewProductItem.php?skuCode=$row[0]>View</a>
</td></tr>
HTML;
You forgot the quotes around the <br/>s , try:
echo("<tr><td>".$row[0].
"<br/>" .
$row[1].
"<br/>". $row[2].
"<br/>" .
"<img src=images/".$row["skuCode"]."_1.jpg>".
"<br/>".
"View".
"</td></tr>");
Or alternatively:
echo("<tr><td>{$row[0]}
<br/>
{$row[1]}
<br/> {$row[2]}
<br/>
<img src=images/.$row[skuCode]._1.jpg>
<br/>
<a href=viewProductItem.php?skuCode={$row[0]}>View</a>.
</td></tr>");
You forgot to open and close the quotes in your string:
<?php
echo "<tr><td>".$row[0]."<br/>".$row[1]."<br/>".$row[2]."<br/><img src=images/".$row["skuCode"]."_1.jpg><br/>View</td></tr>";
?>
Try this:
echo "<tr><td>{$row[0]}<br/>{$row[1]}<br/>{$row[2]}<br/><img src=\"images/{$row['skuCode']}_1.jpg\"><br/>View</td></tr>";
You forgot to wrap the br tags around brackets, also you can use curly braces to never have to exit out of your string and concat it in php
echo "String with $variable works";
echo "String with {$array['key']} works with curly bracers."
If you want to make its HTML output well-formatted, then you could check this:
echo "<tr>\n";
echo " <td>" . $row[0] . "<br />\n";
echo " " . $row[1] . "<br />\n";
echo " " . $row[2] . "<br />\n";
echo ' <img src="images/' . $row["skuCode"] . '_1.jpg"><br />' . "\n";
echo ' View</td><br />' . "\n";
echo "</tr>\n";
Or this:
echo "<tr>\n";
echo " <td>" . $row[0] . "<br />\n";
echo " " . $row[1] . "<br />\n";
echo " " . $row[2] . "<br />\n";
echo " <img src=images/" . $row["skuCode"] . "_1.jpg><br />\n";
echo " View</td><br />\n"
echo "</tr>\n";
Or if you did not like those, look at this:
echo("<tr><td>" . $row[0] . "\n" .
"<br />\n" .
$row[1] . "\n" .
"<br />" . $row[2] . "\n" .
"<br />\n" .
"<img src=images/" . $row["skuCode"] . "_1.jpg>\n" .
"<br />\n" .
"View\n" .
"</td></tr>\n");
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 }} ?>