I've a problem that i can't solve. I think it's an easy fix, but after 3 hours of searching and trail-error. I've decided to ask the question over here:
This is my time function for a schedule application.
date_default_timezone_set('Europe/Amsterdam');
$current_time = time();
$unixtime = $current_time;
$time = date("Gi",$unixtime);
global $time;
function time_left($time, $active_class, $maxtime, $mintime){
if($time < $maxtime and $time > $mintime){
global $active_class;
$active_class = 'active';
echo 'succes!';
}
}
Here is my foreach loop, i loop through an array
foreach($rows as $row){
switch($hours){
case 1:
$t = '8:45 - 9:15';
$mintime = 845;
$maxtime = 914;
time_left($time, $maxtime, $mintime);
break;
case 2:
$t = '8:45 - 9:15';
$mintime = 845;
$maxtime = 914;
time_left($time, $maxtime, $mintime);
break;
/* etc.. etc.. etc... */
}
echo "<li class='" . $active_class ."'>";
echo "<div class='right'></div>";
echo "<div class='hour'>", $times, "</div><span class='divider-time'>|</span>";
$hours++;
$i = 0;
foreach($row[$day] as $r[1]){
$i++;
if ($i == 1) {
$class = 'vk';
} elseif ($i == 2) {
$class = 'lok';
} elseif ($i == 3) {
$class = 'doc';
}
echo "<span class='" . $class . "'>", $r[1], "</span>";
}
echo "</li>";
$class++;
}
I get the 'succes!' echo on the right location. But the active class is not working properly. The idea behind it is that the active class is only shown on one row. Now it searches for a match and everything behind it also gets the active class.
Thanks in advanced.
You should restart the $active_class variable in every iteration.
Otherwise, once it is set to active it won't change its value again.
foreach($rows as $row){
$active_class = '';
//YOUR CODE HERE
....
}
Related
$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
if($value==$book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
echo "</div>";
Here foreach loop can run as many time as it can as well as for loop also, but i want to show the links that are not matched with the foreach value and for loop value.
The foreach loop values like 7:20 am not from database but the for loop value like 7:20 am is from database so if 7:20 am==7:20 am then the if statement it run it is working fine, but the issue is that it is running 2 time if i get 2 value in for loop. It should run my div only once.
Use a array to store the item that has shown.
<?php
foreach($array_of_time as $key=>$value)
{
$tag_list = array();
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
// shown, skip
if (isset($tag_list[$book_time]))
{
continue;
}
// mark as show
$tag_list[$book_time] = 1;
if ($value == $book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
}
else
{
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
I'd like to make the following code a for loop to make everything read better, but I can't seem to get the quotes right and end up with a blank page
if ($_POST['week'])
{
$week = $_POST['week'];
}
//or check for a value submitted by the week menu
elseif ($_POST["user_week1"] == "week1") {
$week = "1";
}
elseif ($_POST["user_week2"] == "week2") {
$week = "2";
}
else if ($_POST["user_week3"] == "week3") {
$week = "3";
}
else if ($_POST["user_week4"] == "week4") {
$week = "4";
}
else if ($_POST["user_week5"] == "week5") {
$week = "5";
}
else if ($_POST["user_week6"] == "week6") {
$week = "6";
}
else {
$week = "1";
}
I tried to do:
if ($_POST['week'])
{
$week = $_POST['week'];
}
for ($i = 1; $i<7; $i++)
{
else if ($_POST["user_week'.$i.'"] == "week'.$i.'") {
$week = $i;
}
}
else {
$week = "1";
}
But that didn't work out too well, I tried using double quotes instead of single around the variables, plus '" and "' to no avail.
Can anyone help with this, or point me towards a good resource on single and double quotes for variables?
Are you all igrnoring the fact that the else if mustn`t be there!?
What you should do is the following
$week = "1";
if ($_POST['week']) {
$week = $_POST['week'];
} else {
for ($i = 1; $i < 7; $i++) {
if ($_POST["user_week" . $i] == "week" . $i) {
$week = $i;
break;
}
}
}
elseif ($_POST['user_week' . $i] == 'week' . $i)
above correction should work
When I launch my web page, increment doesn't work correctly!
It should go like this: $i = from 1 to x (0,1,2,3,4,5,6 etc..).
But instead it jumps over every step giving result of (1,3,5,7 etc..).
Why is this code doing this?
<ul class="about">
<?php
$result = mysql_query("SELECT * FROM info WHERE id = 1");
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
$endBioTxt = explode("\n", $bioText);
for ($i=0; $i < count($endBioTxt);)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
$i++;
}
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
?>
</ul>
Output:
Sometext!(right side)
0
1
Sometext2!(right side)
2
3
...
Please DONT do this as other suggested:
for ($i=0; $i < count($endBioTxt); $i++)
do this:
$count = count($endBioTxt);
for ($i=0; $i < $count; $i++) {
}
No need to calculate the count every iteration.
Nacereddine was correct though about the fact that you don't need to do:
$i++;
inside your loop since the preferred (correct?) syntax is doing it in your loop call.
EDIT
You code just looks 'strange' to me.
Why are you doing:
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
???
That would just set $bioText with the last record (bio value) in the recordset.
EDIT 2
Also I don't think you really need a function to calculate the modulo of a number.
EDIT 3
If I understand your answer correctly you want 0 to be in the left li and 1 in the right li 2 in the left again and so on.
This should do it:
$endBioTxt = explode("\n", $bioText);
$i = 0;
foreach ($endBioTxt as $txt)
{
$class = 'left';
if ($i%2 == 1) {
$class = 'right';
}
echo '<li class="'.$class.'"><div>'.$txt.'</div></li>';
echo $i; // no idea why you want to do this since it would be invalid html
$i++;
}
Your for statement should be:
for ($i=0; $i < count($endBioTxt); $i++)
see http://us.php.net/manual/en/control-structures.for.php
$i++; You don't need this line inside a for loop, it's withing the for loop declaration that you should put it.
for ($i=0; $i < count($endBioTxt);$i++)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
//$i++; You don't need this line inside a for loop otherwise $i will be incremented twice
}
Edit: Unrelated but this isn't how you check whether a number is prime or not
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
This code works, please test it in your environment and then uncomment/comment what you need.
<?php
// This is how query should look like, not big fan of PHP but as far as I remember...
/*
$result = mysql_query("SELECT * FROM info WHERE id = 1");
$row = mysql_fetch_assoc($result);
$bioText = $row['bio'];
$endBioTxt = explode("\n", $bioText);
*/
$endBioTxt[0] = "one";
$endBioTxt[1] = "two";
$endBioTxt[2] = "three";
$endBioTxt[3] = "four";
$endBioTxt[4] = "five";
$totalElements = count($endBioTxt);
for ($i = 0; $i < $totalElements; $i++)
{
if ($i % 2)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
}
/*
// This is how you should test if all your array elements are set
if (isset($endBioTxt[$i]) == false)
{
echo "Array has some values that are not set...";
}
*/
}
Edit 1
Try using $endBioTxt = preg_split('/$\R?^/m', $bioTxt); instead of explode.
I am tweaking a facebook album class that loops and echos data. The problem is that when it echos it loops into one column and I want to separate it into 2 columns. This is what it looks like now:
foreach($json->data as $v)
{
echo "<a class='ImageLink' rel='lightbox[photos]' href = '".$v->source."'><img style='margin:10px;' width='110px' src='".$v->picture."' /></a>";
}
I am trying to do something like so:
$count = count($json->data);
$half_count = $count/2;
echo "<ul class='float:left;'>";
$counter = 0;
foreach($json->data as $v)
{
if ($counter == $half_count +1){echo "<ul class='float:left;'>";}
echo "<li>". $v->picture ."</li>";
if ($counter == $half_count){ echo "</ul>";}
$counter++;
}
echo "</ul>";
But when I use the count function on $json->data and echo that it gives me an array. Please help;
"But when I use the count function on $json->data and echo that it gives me an array." <- Count will always return an int.
Try the following correction to your code:
$count = count($json->data);
$half_count = ceil($count / 2); // make sure to ceil to an int. This will have your first column 1 larger than the second column when the count is odd
echo '<ul style="float:left">';
$counter = 0;
foreach($json->data as $v) {
echo '<li>' , $v->picture , '</li>';
$counter += 1;
if ($counter == $half_count && $count != 1) {
echo '</ul><ul style="float:right">';
}
}
echo "</ul>";
I have the script below and on the website http://cacrochester.com, sometimes the contents of the right sidebar load and sometimes they don't. I think it's if it's your first few times on a page, it will say no events scheduled.
I'm completely stumped on why this is happening. The data in the database is not changing.
Let me know if you need me to post anymore code.
Thanks for helping!
<?php
$dummy = 0;
$headingLength = 0;
$descriptionLength = 0;
$shortHeading = 0;
$shortDescription = 0;
$todayYear = date('Y');
$todayMonth = date('m');
$todayDay = date('d');
$events = new dates();
$noEvents = 0;
//get the number of upcoming events
$numEvents = $events->getNumberEvents();
//get the actual events
$results = $events->getRows();
//used if there are not at least 5 events to fill up the event scroller
switch($numEvents) {
case 1: $dummy = 4; break;
case 2: $dummy = 3; break;
case 3: $dummy = 2; break;
case 4: $dummy = 1; break;
}
//loops through all of the events in the table and adds them to the list
foreach($results as $result)
{
$strippedHeading = stripslashes($result['heading']);
$strippedDescription = stripslashes($result['description']);
$headingLength = strlen($strippedHeading);
$descriptionLength = strlen($strippedDescription);
$shortHeading = $strippedHeading;
$shortDescription = $strippedDescription;
$time = strftime("%l:%M %P", $result['time']);
$location = $result['location'];
$startDate = getdate($result['start_date']);
$today = getdate();
//if the events are NOT in the past...
if($startDate >= $today)
{
//if we are at the end of the array, add the class 'last' to the li
if(current($result) == end($result))
{
echo "<li class=\"last\"><h4>".$shortHeading."</h4><h6>$time</h6><h6>$location</h6></li>".PHP_EOL;
}
else
{
echo "<li><h4>".$shortHeading."</h4><h6>$time</h6><h6>$location</h6></li>".PHP_EOL;
}
$noEvents = 1;
}
//if there is not at least 5 events, it repeats the events in the list until there are 5 total
elseif($dummy > 0 && $numEvents > 0)
{
//if the events are NOT in the past...
if($startDate >= $today)
{
//if we are at the end of the array, add the class 'last' to the li
if($dummy == 0)
{
echo "<li class=\"last\"><h4>".$shortHeading."</h4> ".$shortDescription."</li>".PHP_EOL;
$dummy--;
}
else
{
echo "<li><h4>".$shortHeading."</h4> ".$shortDescription."</li>".PHP_EOL;
$dummy--;
}
//if we have 5 events, do not add anymore
if($dummy < 0)
{
break;
}
}
$noEvents = 1;
}
}
//if there are no events, display the no events message
if($noEvents == 0)
{
echo "<li class=\"last\"><h4>No Events Scheduled</h4></li>".PHP_EOL;
}
?>
When you do $startDate >= $today you are trying to compare two arrays, which isn't too good an idea. Just use plain timestamps and it should work fine:
$startDate = $result['start_date'];
$today = strtotime('today');
Also, I'm not sure if this is a typo: current($result) == end($result), but shouldn't it be $results, which is the array name?