How to add a count == 0 to this? - php

I would like to add a count == 0 which if it is the case, just outputs 'please contact us to find out the dates', but I can't seem to get it to work. Please can someone advise?
$count = 0;
$your_repeater = get_field('add_date');
if ($your_repeater) {
while (have_rows('add_date')):
the_row();
$count++;
$my_field = get_sub_field('course_date');
if ($count == 0) {
echo 'please contact us to find out dates';
} else {
echo '';
}
if ($count == 1) {
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($my_field);
if ($expiration_date > $today) {
// echo $my_field .', ';
$date12 = new DateTime($my_field);
$date13 = new DateTime($todays_date);
$diff = date_diff($date12, $date13);
echo '<b>1. Starts on:</b> '.$my_field;
echo '<div class="reddays"> in '.$diff->format("%R%a days.").'<a href="'.get_page_link(
'10'
).'"> Contact us now</a></div>';
//echo 'Contact us to find out more';
} else {
echo '';
}
}
if ($count == 2) {
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($my_field);
if ($expiration_date > $today) {
//echo $my_field .' ,';
$date12 = new DateTime($my_field);
$date13 = new DateTime($todays_date);
$diff = date_diff($date12, $date13);
echo '<b>2. Starts on:</b> '.$my_field;
echo '<div class="reddays"> in '.$diff->format("%R%a days.").'<a href="'.get_page_link(
'10'
).'"> Contact us now</a></div>';
//echo '<img src="' .bloginfo('url').'/wp-content/themes/derbyskillbuild site/images/hourglass.png" />';
} else {
echo '';
}
}
if ($count == 3) {
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($my_field);
if ($expiration_date > $today) {
//echo $my_field .' ,';
$date12 = new DateTime($my_field);
$date13 = new DateTime($todays_date);
$diff = date_diff($date12, $date13);
echo '<b> 3. Starts on:</b> '.$my_field;
echo '<div class="reddays"> in '.$diff->format("%R%a days.").'<a href="'.get_page_link(
'10'
).'"> Contact us now</a></div>';
} else {
echo '';
}
}
if ($count == 4) {
}
if ($count == 5) {
}
echo '</ul>';
endwhile;
}

It appears that you are checking the count way too soon in your code.
You start with a $count = 0;, but then in the while loop you first do $count++. So, at this point, the count is 1. Then a few lines later you check this:
if ($count == 0) {
echo 'please contact us to find out dates';
}
This will never return true, since you just did a $count++ from 0, so at this point it's always at least 1. It seems that this check should be outside of your while loop and you're currently closing the loop way too late.

Related

Showing explore posts in a more sophisticated way

In my last post someone helped me to show my explore posts and it was very helpful. But I wanna show it in a different way.
When I show my news feed or my profile posts and I reach the bottom, it loads more posts. With my previous code, it just loaded all of them and that's a problem because if there a thousand posts they'll all be loaded at once and I don't want that issue. So I wanna set it up in a way that will load more posts as I get to the bottom. It works for my news feed and profile posts but not the explore posts. It immediately gives me the else statement. it's kind of long but I'm guessing the problem is somewhere at the top of method.
public function loadExplorePosts($data, $limit) {
$page = $data['page'];
$userLoggedIn = $this->user_obj->getUsername();
if($page == 1)
$start = 0;
else
$start = ($page - 1) * $limit;
$str = ""; //String to return
$explore_image = $this->con->prepare('SELECT id, body, added_by, date_added, likes, image FROM posts ORDER BY RAND()');
$explore_image->execute();
// $explore_image->store_result();
//$explore_image->bind_result($id, $body, $added_by, $date_added, $likes, $image);
$explore_image_result = $explore_image->get_result();
if ($explore_image_result->num_rows > 0) {
$num_iterations = 0; //Number of results checked (not necasserily posted)
$count = 1;
while ($row = $explore_image_result->fetch_assoc()) {
$id = $row['id'];
$body = $row['body'];
$added_by = $row['added_by'];
$date_time = $row['date_added'];
$imagePath = $row['image'];
if ($userLoggedIn != $added_by) {
//Check if user who posted, has their account closed
$added_by_obj = new User($this->con, $added_by);
if($added_by_obj->isClosed()) {
continue;
}
$user_logged_obj = new User($this->con, $userLoggedIn);
if($user_logged_obj->isFriend($added_by)){
if($num_iterations++ < $start)
continue;
//Once 10 posts have been loaded, break
if($count > $limit) {
break;
}
else {
$count++;
}
if($userLoggedIn == $added_by)
$delete_button = "<button class='delete_button btn-danger' id='post$id'>X</button>";
else
$delete_button = "";
$user_details_query = $this->con->prepare('SELECT first_name, last_name, profile_pic FROM users WHERE username = ?');
$user_details_query->bind_param("s", $added_by);
$user_details_query->execute();
$user_details_query_result = $user_details_query->get_result();
while ($row = $user_details_query_result->fetch_assoc()) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$profile_pic = $row['profile_pic'];
}
?>
<script>
function toggle<?php echo $id; ?>(event){
var target = $(event.target);
if (!target.is('a') && !target.is('button')) {
var element = document.getElementById("toggleComment<?php echo $id; ?>");
if(element.style.display == "block")
element.style.display = "none";
else
element.style.display = "block";
}
}
</script>
<?php
$comments_check = $this->con->prepare('SELECT * FROM comments WHERE post_id = ?');
$comments_check->bind_param("i", $id);
$comments_check->execute();
$comments_check->store_result();
$comments_check_num = $comments_check->num_rows;
//Timeframe
$date_time_now = date("Y-m-d H:i:s");
$start_date = new DateTime($date_time); //Time of post
$end_date = new DateTime($date_time_now); //Current time
$interval = $start_date->diff($end_date); //Difference between dates
if($interval->y >= 1) {
if($interval->y == 1)
$time_message = $interval->y . " yr"; //1 year ago
else
$time_message = $interval->y . " yrs"; //1+ year ago
}
else if ($interval->m >= 1) {
if($interval->d == 0) {
$days = " ago";
}
else if($interval->d == 1) {
$days = $interval->d . "d";
}
else {
$days = $interval->d . "ds";
}
if($interval->m == 1) {
$time_message = $interval->m . "m ". $days;
}
else {
$time_message = $interval->m . "m ". $days;
}
}
else if($interval->d >= 1) {
if($interval->d == 1) {
$time_message = "Yesterday";
}
else {
$time_message = $interval->d . " days ago";
}
}
else if($interval->h >= 1) {
if($interval->h == 1) {
$time_message = $interval->h . "hr";
}
else {
$time_message = $interval->h . "hrs";
}
}
else if($interval->i >= 1) {
if($interval->i == 1) {
$time_message = $interval->i . "min";
}
else {
$time_message = $interval->i . " mins";
}
}
else {
if($interval->s < 30) {
$time_message = "Just now";
}
else {
$time_message = $interval->s . "sec";
}
}
if($imagePath != "") {
$imageDiv = "<div class='postedImage'>
<img src='$imagePath'>
</div>";
}
else {
$imageDiv = "";
}
$str .= "<div class='status_post' onClick='javascript:toggle$id(event)'>
<div class='post_profile_pic'>
<img src='$profile_pic' width='50'>
</div>
<div class='posted_by' style='color:#ACACAC;'>
<a href='$added_by'> $first_name $last_name </a> $user_to $time_message
$delete_button
</div>
<div id='post_body'>
$body
<br>
$imageDiv
<br>
<br>
</div>
<div class='newsfeedPostOptions'>
Comments($comments_check_num)
<iframe src='like.php?post_id=$id' scrolling='no'></iframe>
</div>
</div>
<div class='post_comment' id='toggleComment$id' style='display:none;'>
<iframe src='comment_frame.php?post_id=$id' id='comment_iframe' frameborder='0'></iframe>
</div>
<hr>";
}
?>
<?php
}
} //End while loop
if($count > $limit)
$str .= "<input type='hidden' class='nextPage' value='" . ($page + 1) . "'>
<input type='hidden' class='noMorePosts' value='false'>";
else
$str .= "<input type='hidden' class='noMorePosts' value='true'><p style='text-align: centre;' class='noMorePostsText'><center> No more posts to show! </center></p><br><br>";
}
echo $str;
}
EDIT -----------
I just tried this and the if echo showed up so maybe it's the way I'm trying to output the results:
if ($explore_image_result > 0) {
echo "Found";
} else {
echo "not";
}

Cache API Calls

I have following code to call and echo some API calls from a site.
function popular_uploads() {
// If no saved data exists in the cache
if ($json_data === false) {
// Fetch new data from remote URL
$gameid = $instance['code1'];
$url = ("http://thegamesdb.net/api/GetGame.php?id=".$gameid."");
$json = file_get_contents($url);
$json_data = json_decode($json, false);
set_transient('my_unique_identifier', $json_data, 3600); $json_data = get_transient('my_unique_identifier');
}
foreach ( $json_data->items as $item ) {
$title=$item->Game->GameTitle;
$boxartw=$item->Game->Images->boxart[0];
$boxart=$item->Game->Images->boxart[1];
if ($boxart == NULL) {
$boxart = $boxartw;}
$publisher=$item->Game->Publisher;
$releasedate=$item->Game->ReleaseDate;
$newdate = date("d/m/Y", strtotime($releasedate));
$newday = date("d", strtotime($releasedate));
$newmon = date("m", strtotime($releasedate));
$newyear = date("Y", strtotime($releasedate));
echo '<div class="upcoming_games_side">';
echo '<div class="upcomig_bg"style="background-image:url(http://thegamesdb.net/banners/_gameviewcache/' .$boxart. ');></div>';
echo '<div class="dark-screen-uc"></div>';
echo '<img class="game-boxart_uc" alt="" src="http://thegamesdb.net/banners/_gameviewcache/'.$boxart.'"/>';
if ($title == null){
echo "N/A"; }else{
echo '<h1 class="title_uc">'.$title.'</h1>';}
if ($publisher == null){
echo "N/A"; }else{
echo '<p class="pub_uc">'.$publisher.'</p>';
}
if ($newdate == "01-01-1970"){
echo "N/A";
}else{
echo '<date class="date_uc">'.$newdate.'</date>';
}
$target = mktime(0, 0, 0, $newday, $newmon, $newyear) ;
$today = time () ;
$difference =($target-$today) ;
$days =(int) ($difference/86400) ;
if ($days == "0"){
echo "N/A"; }else{
echo '<date class="count_uc">'.$days.'</date>';}
echo '</div>';
}
}
But it's not working and doesn't Cache API data. So Where is my mistake and wahts part's of my code's are Wrong? I'd appreciate any help.
By the way, it's a Wordpress plugin.

Ajax mouseover show function alignment

I am trying to open a box by using mouseover function, however the box always shows on the right part of the object. Hence when the object is right end of the page, the whole box cannot be seen as shown in this photo.
https://rapidshare.com/files/998669066/problem.png
Here is my ajax code:
$(document).ready(function() {
$('.date-available').click(function() {
alert($(this).attr('id'));
});
$('.rezerve-td').mouseover(function() {
$(this).children().show();
}).mouseout(function() {
$(this).children().hide();
});
});
What should i do in order prevent it? Thanks.
Here is the html part of my code:
require_once 'class.dates.php';
require_once 'class.generic.php';
require_once 'connection.php';
$query = mysql_query("SELECT * FROM egitim_salonu", $baglanti);
while ($row = mysql_fetch_object($query)) {
$salonlar[$row->id] = array('ad' => $row->ad, 'detay' => $row->detay);
}
$query = mysql_query("SELECT * FROM salon_rezervasyonu", $baglanti);
while ($row = mysql_fetch_object($query)) {
$rezervasyonlar[$row->salon_id.'%'.$row->gun.'%'.$row->tip] = array('salon_id' => $row->salon_id, 'gun' => $row->gun, 'tip' => $row->tip, 'rezerve_edildigi_egitim' => $row->rezerve_edildigi_egitim);
}
$month = date('m', strtotime($_POST['d']));
$year = date('Y', strtotime($_POST['d']));
$date = date('F Y', strtotime('01-'.$month.'-'.$year));
if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) {
$numberOfDays = 31;
} else if ($month == 4 || $month == 6 || $month == 9 || $month == 11) {
$numberOfDays = 30;
} else if (date('L', strtotime($year.'-01-01'))) {
$numberOfDays = 29;
} else {
$numberOfDays = 28;
}
// Tabloyu aç, başlığı hazırla
$output = '<table class="calendar" id="salon-rezervasyon-table">
<thead>
<tr><th rowspan="2">Salon</th><th colspan="'.(2 * $numberOfDays).'">'.$date.'</th></tr>
<tr>';
for ($i = 1; $i <= $numberOfDays; $i++) {
$output .= '<th width="10" colspan="2" class="day-th">'.$i.'</th>';
}
// Başlığı kapat, gövdeyi aç
$output .= '</tr></thead><tbody>';
// Her salon için...
foreach ($salonlar as $salonKey => $salonValue) {
$output .= '<tr id="salon-'.$salonKey.'"><td class="calendar-salon-column">'.$salonValue['ad'].'<br />'.$salonValue['detay'].'</td>';
// ... o ay içindeki günleri kontrol et. Burada kontrol için şunu yapıyoruz: salon id'si, tarih ve gün içerisindeki saati (öğleden önce, öğleden sonra)
// birleştirerek bir index (bu index unique bir index) oluşturuyoruz ve bu index yukarıdaki rezervasyonlar dizisinde tanımlanmış mı diye kontrol ediyoruz.
for ($i = 1; $i <= $numberOfDays; $i++) {
$dateCheck = date('Y-m-d', strtotime($i.'-'.$month.'-'.$year));
$rIndexOO = $salonKey.'%'.$dateCheck.'%Öğleden önce';
$rIndexOS = $salonKey.'%'.$dateCheck.'%Öğleden sonra';
//Öğleden önce
if(isset($rezervasyonlar[$rIndexOO])) {
$egitimPlaniQuery = mysql_query("SELECT * FROM egitim_plani WHERE id = ".$rezervasyonlar[$rIndexOO]['rezerve_edildigi_egitim'], $baglanti);
$egitimPlani = mysql_fetch_object($egitimPlaniQuery);
$output .= '<td id="'.$dateCheck.'%o" class="calendar-td rezerve-td">';
$output .= '<div class="rezerve">'.date('d-m-Y', strtotime($dateCheck)).' Öğleden Önce<br /><br />'.$egitimPlani->egitim_adi.'</div>';
$output .= '</td>';
} else {
$output .= '<td id="'.$dateCheck.'%o" class="date-available calendar-td">';
$output .= '';
$output .= '</td>';
}
//Öğleden sonra
if(isset($rezervasyonlar[$rIndexOS])) {
$egitimPlaniQuery = mysql_query("SELECT * FROM egitim_plani WHERE id = ".$rezervasyonlar[$rIndexOO]['rezerve_edildigi_egitim'], $baglanti);
$egitimPlani = mysql_fetch_object($egitimPlaniQuery);
$output .= '<td id="'.$dateCheck.'%s" class="calendar-td rezerve-td">';
$output .= '<div class="rezerve">'.date('d-m-Y', strtotime($dateCheck)).' Öğleden Sonra<br /><br />'.$egitimPlani->egitim_adi.'</div>';
$output .= '</td>';
} else {
$output .= '<td id="'.$dateCheck.'%o" class="date-available calendar-date-separator">';
$output .= '';
$output .= '</td>';
}
}
$output .= '</tr>';
}
$output .= '</tbody></table>';
echo $output;
You could probably fix this with some css.
div.rezerve {
position: absolute
}
If it still shows up off the page, you can add something like this to your javascript
....
var doc_width = $(document).width();
var rezerve_child = $(this).children();
rezerve_child.show();
var position = rezerve_child.offset();
var rezerve_child_width = rezerve_child.width();
if (position.left + rezerve_child_width > doc_width) {
$(this).css('right', '2px');
}
....
The jQuery Tooltip plugin would also probably be helpful.
http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/

Separating single grouped WHILE loop results

I asked a question yesterday which was solved and I can now output WHILE loops which 2 results on a line, rather than 1 on a line. This is the code I've got at the moment:
if(mysql_num_rows($result2) > 0) {
$count=0;
$day = 1;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "<b>";
if ($day=='1') { echo "Sunday - ";}
else if ($day=='3') { echo "Monday - "; }
else if ($day=='5') { echo "Tuesday - "; }
else if ($day=='7') { echo "Wednesday - "; }
else if ($day=='9') { echo "Thursday - "; }
else if ($day=='11') { echo "Friday - "; }
else if ($day=='13') { echo "Saturday - "; }
else { echo "";}
if (($row["open"] == 0) && ($row["close"] == 0))
{
echo "closed"
}
else
{
echo "</b>" . $row["open"] . "-" . $row["close"] . " ";
if ($count % 2 !=0 ){ echo "<br/><br/>";}
}
$count++;
$day++;
}
} else {
echo "Error";
}
With this code, when the branch is closed, then it returns the word 'closed' twice, where I need it to appear only once. Is this possible?
Just test on modulo the value within $day :
in place of :
if (($row["open"] == 0) && ($row["close"] == 0)) {
echo "closed";
}
do :
if ($row["open"] == 0 && $row["close"] == 0) {
if ($day % 2 == 1) {
echo "closed";
}
}
It may not be the most elegant solution but I would probably have a variable $closed = false, which gets set to true if the branch is closed. Then just test for $closed, if it's false, output "closed".
Use a for loop:
for($i=0`;`$i`<`count(row)`;`$i++)
{
}

ForEach loop: Output something different on every second result

I have two for each loops and I am trying to output something different for each second result:
foreach ($wppost as $wp) {
$wp_title = $wp->post_title;
$wp_date = strtotime($wp->post_date);
$wp_slug = $wp->post_name;
$wp_id = $wp->ID;
// Start Permalink Template
$wp_showurl = $wp_url;
$wp_showurl = str_replace("%year%", date('Y', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%monthnum%", date('m', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%day%", date('d', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%hour%", date('H', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%minute%", date('i', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%second%", date('s', $wp_date), $wp_showurl);
$wp_showurl = str_replace("%postname%", $wp_slug, $wp_showurl);
$wp_showurl = str_replace("%post_id%", $wp_id, $wp_showurl);
// Stop Permalink Template
$wp_posturl = $blog_address . $wp_showurl;
echo '<li>'.$wp_title.'</li>';
}
For this one I want it to echo <li class="even"> instead of just <li> for each second result. I think this will have to be changed to a for loop to accomplished this but I am not sure how that is done without breaking it.
Exact same for this one:
if(is_array($commenters)) {
foreach ($commenters as $k) {
?><li><?php
if($ns_options["make_links"] == 1) {
$url = ns_get_user_url($k->poster_id);
if(trim($url) != '')
{
echo "<a href='" . $url . "'>";
}
}
if($ns_options["make_links"] == 2) {
$url = ns_get_user_profile($k->poster_id);
echo "<a href='" . $url . "'>";
}
$name = $bbdb->get_var("
SELECT user_login
FROM $bbdb->users
WHERE ID = $k->poster_id");
echo ns_substr_ellipse($name, $ns_options["name_limit"]);
if(trim($url) != '' && $ns_options["make_links"] == 1) {
echo "</a>";
}
if($ns_options["make_links"] == 2) {
echo "</a>";
}
if ($ns_options["show_posts"] == 1)
{
echo " (" . $k->num_posts . ")\n";
}
echo $ns_options["end_html"] . "\n";
unset($url);
}
} else {
?></li><?php
}
Thanks,
Wade
You can create a "counting" variable, and then use the result of $count % 2 to determine if it is an odd or even row:
$rowCount = 0;
foreach ($wppost as $wp) {
// stripping stuff:
echo '<li';
// increase $rowCount - if the remainder of a division by 2 is 1, echo extra class info:
if ($rowCount++ % 2 == 1 ) echo ' class="even"';
echo '>'.$wp_title.'</li>';
}
if(is_array($commenters)) {
$commentCount = 0;
foreach ($commenters as $k) {
?><li<?php echo ($commentCount++%2==1)?' class="even"':''?>><?php
//stripped the rest of the loop:
}
}
I have done something like this, maybe this will help someone who is searching for this type of solutions.
#foreach ($occasions as $key => $occasion)
#if ($key++ % 2 == 1)
<li style="background: #f6f6f6;">
#else
<li>
#endif
{{$occasion->name}}</li>
#endforeach

Categories