If statement to use <a href one> else use <a href two> - php

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>

Related

PHP variable in CSS Class

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 ==

Foreach pagination

I am looking for help in how to paginate my foreach output. I've looked at other questions and answers and cannot find a solution that works for me or that I can figure out on my own. Right now my code, which is below, outputs everything into table rows. My problem, of course, is that it dumps all data on a single page -- hence the reason I want pagination. I want to paginate for every 11 items on the page. The page is a magazine archive, and there are 11 issues pear year -- so every page is equal to 1 year of our magazine. The first page should host issues 1-11 and page two should host issues 12 through 22, etc. We have 10 years worth of magazine issues. Any help would be greatly appreciated. Thank you!
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<strong><?php echo $cat->name; ?></strong><br>
<em><?php echo $cat->description; ?></em><br>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
</td>
<?php endforeach; ?>
</tr>
</table>
What framework/ application is this? While I don't understand 100% where the data is coming from, you could try modifying your code to add a break when you hit 11 entries (or 10 when you are starting from 0).
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
...
<?php if(10 == $col) { break;}
endforeach; ?>
That will limit this to the first 11 results of your foreach loop. From there, you would have to modify how you select the data so that the second page starts at record 12.
Of course, it may just be easier to use limit syntax on your sql query, eg:
limit 12, 22
I think this is what you want:
get_terms('term', array('offset' => $page * 11, 'number' => 11));
Where $page starts at 0 for the first page. If you want page to start at 1, use ($page-1) * 11.
How you determine what page to show is up to you.
See the get_terms documentation for more information: http://codex.wordpress.org/Function_Reference/get_terms
this may not be what you were looking for or expecting but hope you or someone else finds useful.
the following script does everything small configuration required ps_pagination.php
You need to include the ps_pagination.php into the pages where you want to use it and then use the following code to populate the information on the paginated pages.
Hope its helpful.
db_connection = mysql_connect('localhost', 'user', 'password')or die("cannot connect");
mysql_select_db('database',$conn);
// mysql query
$sql_query="SELECT * FROM example ORDER BY id DESC";
// Create the ps_pagination object here
$pager = new ps_pagination($db_connection,$sql_query,10,5);
//The paginate() function returns a mysql result set
$rs = $pager->paginate();
while($rows = mysql_fetch_assoc($rs)) {
// table to display results here // modify here
echo $rows["title"].'</p>';
echo '<p> '.$rows["post"].'</p>';
echo '<p><span class="style1">By</span>: '.$rows["name"].' ';
echo '<p>Date/Time</span>:'.$rows["datetime"].'</p>';
echo "<BR>";
}
// close mysql connection here
mysql_close();
//Display the full navigation in one go
echo $pager->renderFullNav();
Here is the code I used to solve my problem:
<?php
$url = $_SERVER["REQUEST_URI"];
$segments = explode('/', $url);
$page = is_numeric($segments[count($segments)-2]) ? $segments[count($segments)-2] : 1;
$next = $page + 1;
$prev = $page - 1;
$issues_per_page = 11;
$lastpage = ceil(wp_count_terms( 'mag') / $issues_per_page) ;
?>
<?php wp_count_terms( 'mag' ); ?>
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('mag', array('offset' => ($page - 1) * $issues_per_page, 'number' => $issues_per_page)) as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<strong><?php echo $cat->name; ?></strong><br>
<em><?php echo $cat->description; ?></em><br>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
</td>
<?php endforeach; ?>
</tr>
</table>
<?php if ($prev > 0) : ?>
Previous
<?php endif ?>
<?php if ($page < $lastpage) : ?>
Next
<?php endif ?>

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

Show Ads In Middle Of Results

I'm currently using sphider on one of my websites, my questions is how can I break the results page into 2 parts to add a 200px break to place a ad slot.
Code:
<?php
extract($search_results);
?>
<?php if ($search_results['did_you_mean']){?>
<div id="did_you_mean">
<?php echo $sph_messages['DidYouMean'];?>: <?php print $search_results['did_you_mean_b']; ?>?
</div>
<?php }?>
<?php if ($search_results['ignore_words']){?>
<div id="common_report">
<?php while ($thisword=each($ignore_words)) {
$ignored .= " ".$thisword[1];
}
$msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]);
echo $msg; ?>
</div>
<?php }?>
<?php if ($search_results['total_results']==0){?>
<div id ="result_report">
<?php
$msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]);
echo $msg;
?>
</div>
<?php }?>
<?php if ($total_results != 0 && $from <= $to){?>
<div id ="result_report">
<?php
$result = $sph_messages['Results'];
$result = str_replace ('%from', $from, $result);
$result = str_replace ('%to', $to, $result);
$result = str_replace ('%all', $total_results, $result);
$matchword = $sph_messages["matches"];
if ($total_results== 1) {
$matchword= $sph_messages["match"];
} else {
$matchword= $sph_messages["matches"];
}
$result = str_replace ('%matchword', $matchword, $result);
$result = str_replace ('%secs', $time, $result);
echo $result;
?>
</div>
<?php }?>
<?php if (isset($qry_results)) {
?>
<div id="results">
<!-- results listing -->
<?php foreach ($qry_results as $_key => $_row){
$last_domain = $domain_name;
extract($_row);
if ($show_query_scores == 0) {
$weight = '';
} else {
$weight = "[$weight%]";
}
?>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
<div class="idented">
<?php }?>
<b><?php print $num?>.</b> <?php print $weight?>
<?php print ($title?$title:$sph_messages['Untitled'])?><br/>
<div class="description"><?php print $fulltxt?></div>
<div class="url"><?php print $url2?> - <?php print $page_size?></div>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
[ More results from <?php print $domain_name?> ]
</div class="idented">
<?php }?>
<br/>
<?php }?>
</div>
<?php }?>
<!-- links to other result pages-->
<?php if (isset($other_pages)) {
if ($adv==1) {
$adv_qry = "&adv=1";
}
if ($type != "") {
$type_qry = "&type=$type";
}
?>
<div id="other_pages">
<?php print $sph_messages["Result page"]?>:
<?php if ($start >1){?>
<?php print $sph_messages['Previous']?>
<?php }?>
<?php foreach ($other_pages as $page_num) {
if ($page_num !=$start){?>
<?php print $page_num?>
<?php } else {?>
<b><?php print $page_num?></b>
<?php }?>
<?php }?>
<?php if ($next <= $pages){?>
<?php print $sph_messages['Next']?>
<?php }?>
</div>
<?php }?>
<div class="divline">
</div>
I'm also not aware of a live PHP code editor, if you know of one please comment and share so I can add a link!
Presuming $from and $to are the result numbers, so you're displaying "Showing results 10 to 30 of 100" for example:
<div id="results">
<!-- results listing -->
<?php $adbreak = ($to - $from) / 2;
<?php foreach ($qry_results as $_key => $_row){
<?php if ($adbreak == 0) { ?>
<div id="results-adbreak">
<img src="buy-a-car.jpg" alt="one careful owner!" />
</div>
<?php }
$adbreak--;
?>
// rest of your code
This will put a div approximately (give or take one) half way down your page of results. You can obviously replace the ad with a call to whatever you want.
adding something like:
<?php $adbreak = ($to - $from) / 2;
<?php if ($adbreak < 5) $adbreak = -1; ?>
will ensure that it doesn't display at all if the results list is too short.
If you don't know $to and $from in advance, you can still do it, but you'll have to calculate the equivalent from the query result first.

What can I do when Text = NULL?

<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php do { ?>
<?php echo $row_Recordset1['Name']; ?>:
<?php echo $row_Recordset1['Text']; ?>
•
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>
Print a friendly message to the user instead of NULL:
<?php echo (NULL === $row_Recordset1['Text']) ? "No value" : $row_Recordset1['Text']; ?>
As xil3 illustrates, you can also use this pattern (from the docs):
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
The way you have it written right now, $row_Recordset1 will be null the first time it goes into the loop.
I've rewritten it for you:
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
<?php echo (($row_Recordset1['Name'] != null) ? $row_Recordset1['Name'] : 'n/a'); ?>:
<?php echo (($row_Recordset1['Text'] != null) ? $row_Recordset1['Text'] : 'n/a'); ?>
•
<?php } ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>

Categories