I am trying to select a CSS class based on the php variable. This is for an estate agentcy. Here is my code but it does not work and I am not sure why:
<td width="130" height="30" align="center" class="propertyStatus">
<?php
$pStatus = "notAvailable";
if($property->status = 'Available') {$pStatus = "available";}
?>
<span class="<?php echo $pStatus ?>"><?php echo $property->status ?></span>
</td>
It always shows as "Avaible" rather than Sold when properties are sold.
Help is apreciated.
it should be if($property->status == 'Available')
because $property->status = 'Available' <= this assigns the values and doesn't compare it!!
what your are doing currently is assigning the value whereas you have to compare the 2 strings!!
so it should be
<?php
$pStatus = "notAvailable";
if($property->status == "Available") /*double equal to sign here*/
{
$pStatus = "available";
}
?>
Use == instead of = and it should be,
if($property->status == 'Available') {$pStatus = "available";}
It's enough to do something like this:
<span class="<?php echo strtolower($property->status); ?>">
But yes, the problem is on the = instead of ==
Related
I have this loop at the same it will count the output:
while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++;
Then to call the proper html class I need this for my design:
<div class="span4 <?php if($current == 0) { echo 'first'; } elseif($current == 1) { echo 'second'; } elseif($current == 2) { echo 'first'; } elseif($current == 3) { echo 'second'; } ?>" id="<? echo $current; ?>">
The $count starts from 0 and $count2 from 1. The output should be like this: if 0=first, 1=second, 2=first, 3=second and so forth. How can I make this expression shorter with unlimited number of $count? I hope my question is clear. Thank you for those who will help me here.
If I understand what you are asking here, I think you want to alternate your divs with'first' and 'second' classes?
As such, you can use the % modulo operator (see http://php.net/manual/en/internals2.opcodes.mod.php)
<div class="span4 <?php echo ($current % 2 === 0 ? 'first' : 'second'); ?>" id="<? echo $current; ?>">
If you are just using these class names for alternating CSS styles there is a much elegant way to do this using CSS3
Your HTML
<div class="span4" id="<? echo $current; ?>">
And in your css file
.span4:nth-child(even) {background: #CCC}
.span4:nth-child(odd) {background: #FFF}
else for a PHP solution the answer by Liam Wiltshire should work good.
source: CSS even and odd rules examples
So you could use mod - best to use the full<?php tag for WordPress if that is what it is.
php test if number is odd or even
// set up the html clips
if($current % 2 == 0){
$html_clips = 'first';
}else{
$html_clips = 'second';
}
if($current == 0) $html_clips = 'first';
<div class="span4 <?php echo $html_clips ?>" id="<?php echo $current; ?>">
try this way
<div class="span4
<?php if ($curent%2 == 0){ echo 'first'; }
else {echo 'second';}?> id="<? echo $current; ?>">
What I'm trying to do is that any form with the $id that is less than 50 to use a certain A href else use another A HREF. This is what I have so far but I have no idea how to include it since it has php with in the HREF. How can I make this work. Thanks in advance.
</td>
<td>
<?php if($id <= 50) {
<?=$row[software]?>
}
else {
<?=$row[software]?>
}
?>
</td>
<td>
<?php if($id <= 50): ?>
<?=$row[software]?>
<?php else: ?>
<?=$row[software]?>
<?php endif; ?>
</td>
A cleaner way to do this would be to just use an if statement on the part of the anchor tag that changes (the modify/template bit):
<?=$row[software]?>
You can put this anywhere in a page that is parsed by PHP (doesn't matter that there's inline PHP in your HTML.
Exit PHP like this (easy enough - in your case you don't need to edit really, you just wrap the top example here in your <td> tags):
?>
<?=$row[software]?>
<?php
... or:
$action = $id <= 50 ? 'template' : 'modify';
echo '' . $row['software'] . '';
<?php
if($id <= 50) {
echo '' . $row['software'] . '';
} else {
echo ''. $row['software'] . '';
}
?>
printf() is your friend.
<td>
<?php
$frame = "%s";
if($id <= 50) {
printf($frame, $row[form_type], $row[id], $row[form_type], '***template***', $row[software]);
} else {
printf($frame, $row[form_type], $row[id], $row[form_type], '***modify***', $row[software]);
}
?>
</td>
</td>
<td>
<?php
if($id <= 50) {
echo ''.$row[software].'';
} else {
echo ''.$row[software].'';
}
?>
First of all, don't use the short tags <?=?>, this is considered bad practice.
Secondly, you may use this:
</td>
<td>
<?php if($id <= 50) { ?>
<?php echo $row[software]?>
<?php } else { ?>
<?php echo $row[software]?>
<?php } ?>
</td>
I Have a ordered list and i want to generate a incemental number for the data-slide-to starting from 0
That is the php code
get("display_indicators", 1)): ?>
$item){
$activeclass = "";
if($key == 0){
$activeclass = "active";
}
?>
id;?>" data-slide-to="" class="">
You could use a variable to contain a counter...
<?php $counter = 0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?php
echo $counter;
$counter++;
?>" class="<?php echo $activeclass; ?>"></li>
You could even use the following instead of echo-ing and incrementing on two lines...
echo $counter++;
This echoes the current count and increments afterwards, but some people prefer to avoid this.
Try this, hope it'll help you
<?php $uniqueNo=0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="
<?php echo $uniqueNo+=1; ?>" class="<?php echo $activeclass; ?>"></li>
Use an incremential counter
<?php $counter = 0; ?>
<ul>
enter code here
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?=$counter++?>"
class="<?php echo $activeclass; ?>"></li>
this however could not be "unique" application wide
I'm not really sure what the problem is here, I have a posts table that has a foreign key of userid and I want to pull their usernames from the users table based on that, it seems to pull them out fine but it won't display on the first post.
My code is:
$query_therealuserid = 'SELECT users.username, users.userid, posts.userid, posts.created_at
FROM users, posts
WHERE users.userid = posts.userid
ORDER BY posts.created_at';
$therealuserid = mysql_query($query_therealuserid, $akaearthdb) or die(mysql_error());
and
<?php do { ?>
<div id= "post">
<?php if (stripos($row_rsjustpost['link'], ".png") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".gif") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpeg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php echo $row_rsjustpost['title']; ?>
<br/>
<?php echo $row_rsjustpost['text']; ?>
</p>
<br />
<?php
do {
if ($whileLoopCounter0!=$whileLoopCounter1){break;}
?>By: <?php echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
} while($row = mysql_fetch_array($therealuserid));
?>
</div>
<?php $whileLoopCounter0++; ?>
<?php } while ($row_rsjustpost = mysql_fetch_assoc($rsjustpost)); ?>
</div>
when I pull up the page I get all the posts but the first one doesn't have a username and the usernames are all pushed down one post. The first post has a postid of 2, if I create a new post with a postid of 1 and a userid it shows up at the top without a username and the others usernames are moved up so that they are corect.
That's because you use a do-while loop, the $row = mysql_fetch_array($therealuserid) is executed at the end of the loop, why not use a regular while loop.
You're currently using a do-while loop, so first the do block is executed and then the while block is evaluated only after the 1st execution of the do block.
Use this instead:
while ($whileLoopCounter0==$whileLoopCounter1) {
$row = mysql_fetch_array($therealuserid);
if ($row){
?>
By:
<?php
echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
}
}
Looks like you are fetching the username after you increment the loop counter. The user name is pulled after the loops is one, so the first row will be empty because that data doesn’t exist yet.
There are a lot of other things though that you’re doing that are just wasting effort though. Try tossing the results into an object and using foreach($result as $key=>$value) or foreach($result as $item) if you do not need the key instead.
I've got a site where someone searches for x product in their location and the site spits back a list of results.
if(isset($_POST['zip'])){
$qry="SELECT business_id FROM ".TBL_BUSINESS." WHERE zip LIKE '%".$_POST['zip']."%'";
$rs = mysql_query($qry);
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE ){
$rec[] = $row[0];
}
if(!empty($rec[0])){
echo "Products for this location<br/>";
foreach ($rec as $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="norm">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
}
else
{
echo "No Product is added for this location";
}
}
?>
What would be the best way to alternate <div class="norm"> with <div class="alt">?
keep a counter and use it's value modulo 2 to determine whether the class should be "norm" or "alt".
$rec2 = mysql_fetch_array($rs2);
$count++;
?>
<div class="<?php echo($count%2?"norm":"alt"); ?>">
I tend to use something like this:
$row = 0;
foreach ($rec as $result) {
$class = $row++ & 1 == 1 ? 'alt' : 'norm';
...
echo <<<END
<div class="$class">
...
END;
}
You can use curly braces to do the expression within the string but I generally prefer not to embed that kind of logic. It's (imho) a little harder to read. Plus the above gives you the opportunity to output it for debugging purposes more easily, etc.
if(some expression)
{
$class="norm";
}
else
{
$class="alt";
}
?>
<div class="<?php echo $class;?>">
set a counter on your output loop. Whenever the counter is even, set the class to normal, else set it to alternate.
Why not use modulus and a row id? Much simpler, no need for unnecessary variables
foreach ($rec as $rid => $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="<?=($rid % 2 == 0) ? 'norm' : 'alt' ?>">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
Widespread adoption of CSS 3 can't come soon enough: http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#structural-pseudos