Forum Moderators: coopster

Message Too Old, No Replies

php - add echo if status sold

         

actolearn

9:53 pm on Apr 8, 2015 (gmt 0)

10+ Year Member



Hello - I need help with my php so it inserts the word SOLD if product status is "sold". Needs to be next to my product_price below.

<?php
//select items from db
$items = mysql_query ("SELECT * FROM db_table_name WHERE category='apples' AND status=('active' OR 'sold') ORDER BY status='active' DESC, product_number DESC") or die(mysql_error());

//extract every item
while($item = mysql_fetch_array($items))
{
?>

<figure>
<a href="http://www.example.com/<?php echo ($item["product_url"]); ?>">
<img src="http://www.example.com/<?php echo ($item["product_img"]); ?>" ></a>
<figcaption>
<a href="http://www.example.com/<?php echo ($item["product_url"]); ?>">
<h3><?php echo ($item["product_name"]); ?></h3></a>
<?php echo ($item["product_price"]); ?> ..... insert SOLD here if that's the status
</figcaption></figure>

<?php
}

?>

Demaestro

10:33 pm on Apr 8, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This will do it.

<?php echo ($item["status"] == 'sold' ? 'Sold' : '' ); ?>

What this does is it checks if the status is 'sold' if it is it will echo 'Sold' else it will echo ''

This is called a Ternary operator. Everything before the ? is the condition. After between the ? and : is executed if the condition is met... everything after the : is executed if the condition is not met.

Basically this says

if (status == 'sold') {
echo 'Sold';
} else {
echo '';
}

actolearn

10:51 pm on Apr 8, 2015 (gmt 0)

10+ Year Member



Thanks so much - that worked! One more question - Is there any way of making Sold red in color with this php or will I have to stick with my css for that?

lucy24

11:23 pm on Apr 8, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there any way of making Sold red in color

Once you're using the "echo" statement, you can echo any text you want. Pro tip: Don't use the word "red"; you might change your mind later. Do something like
<span class = "superemphatic">Sold</span>
span.superemphatic {color: red;}
That was an HTML/CSS suggestion, nothing to do with the php as such.

actolearn

2:38 pm on Apr 9, 2015 (gmt 0)

10+ Year Member



Adding this part in for anyone else like me who needs help with the basic stuff.

To conclude, I needed the following:
price (and SOLD when applicable) on same line/centered/SOLD in different color

Searched for answer to my second question and ended up with the following after fixing up my stylesheet:

<div class="prcolor">
<?php echo ($item["prodprice"]); ?>
<span>
<?php echo ($item["prodstat"] == 'sold' ? 'SOLD' : '' ); ?>
</span></div>

I love this forum!

lucy24

5:37 pm on Apr 9, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What's the naked <span> for? Does the CSS say
div.prcolor span {blahblah}
? (Perfectly legitimate, but looks funny.)

More seriously, wouldn't this coding leave your html with a bunch of empty
<span>
</span>
for all the products that have not been sold? Seems wasteful. (That is, you're sending content to the browser that it doesn't need.)

actolearn

7:50 pm on Apr 9, 2015 (gmt 0)

10+ Year Member



How would I get same results but only when item is sold? I get your point, Lucy, but can't figure out how to do that.

lucy24

8:23 pm on Apr 9, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remember, you can echo anything. (penders or someone like him will step in and point out the horrible exceptions). So you can perfectly well say
echo "<span>Sold</span>"
The command "echo" doesn't mean "display this text where a human can see it". It means "put this text in the raw HTML". (This is why I echo things like \n that serve absolutely no purpose. I'm obsessive about making everything look hand-rolled.)

actolearn

3:11 pm on Apr 10, 2015 (gmt 0)

10+ Year Member



Took me awhile. Shows up correctly on my web page now. With all the dots, parens, etc. and placement thereof, hard for me to figure out.

Seems to be different ways to do this. Have I got it right and best way NOW?

<div class="prcolor">
<?php echo ($item["prodprice"]); ?>
<?php echo '<span>' . ($item["prodstat"] == 'sold' ? 'SOLD' : '' ) . '</span>'; ?>
</div>

LifeinAsia

4:07 pm on Apr 10, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That's not really any different from what you previously had. I think what Lucy was suggesting was something like:
<?php echo ($item["prodstat"] == 'sold' ? '<span>SOLD</span>' : '' ); ?>

topr8

4:12 pm on Apr 10, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i think what was suggested was this:

<?php echo ($item["prodstat"] == 'sold' ? '<span class="yadda">SOLD</span>'): '' ); ?>

actolearn

4:25 pm on Apr 10, 2015 (gmt 0)

10+ Year Member




That's not really any different from what you previously had. I think what Lucy was suggesting was something like:
<?php echo ($item["prodstat"] == 'sold' ? '<span>SOLD</span>' : '' ); ?>

Thanks - I tried every variation but the best and simplest. I really need to get out of my own way.

actolearn

4:58 pm on Apr 10, 2015 (gmt 0)

10+ Year Member



What's the naked <span> for? Does the CSS say
div.prcolor span {blahblah}

<style>
.prodprice {text-align: center;}
.prodprice span {margin-left: .875em; color: #cd5c5c;}

</style>

<figure>
<a href="http://www.example.com/<?php echo ($item["produrl"]); ?>">
<img src="http://www.example.com/<?php echo ($item["galimg"]); ?>" alt="" width="225" height="168"></a>
<figcaption>
<a href="http://www.example.com/<?php echo ($item["produrl"]); ?>">
<h3><?php echo ($item["prodnm"]); ?></h3>
</a>
<div class="prodprice">
<?php echo ($item["prodprice"]); ?>
<?php echo ($item["status"] == 'sold' ? '<span>SOLD</span>' : '' ); ?>
</div>
</figcaption>
</figure>

actolearn

5:01 pm on Apr 10, 2015 (gmt 0)

10+ Year Member



I put the span inside the div because it was the only way to get the sometimes "SOLD" to stay on the same line as the always "price".

topr8

6:00 pm on Apr 10, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... OT, but personally i don't leave the price up when items are sold - but my niche is different to yours

lucy24

7:43 pm on Apr 10, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I put the span inside the div because it was the only way to get the sometimes "SOLD" to stay on the same line as the always "price".

That part makes perfect sense; I just wondered about the nameless span, since spans (and divs) are generally used as holders for some class. But if only one span can ever occur inside this particular div, then there really is no reason to clutter up your html with a class name.