display weekdays on the calendar - php

I am having a problem. I can't display the weekdays.
So when I click next it should display the week for nextweek displaying Monday to Friday.
And when I click previous it should display lastweek Monday to Friday
here's my code
in controller
<?php
public function calendar($dt) {
// = date("Y/m/d");
$beginDate = $dt;
$endDate = date("Y-m-d", strtotime($dt . "+6 days"));
$posts = $this->Post->getWeekPosts($beginDate, $endDate);
$members = $this->Post->PostgetUniqueMembers($posts);
// $week = $this->Post->weekNo($beginDate, $endDate);
$this->set("posts", $posts);
$this->set("members", $members);
$this->set("begin", $beginDate);
// $this->set("week", $week);
}
in view
<div id="content">
<span id="calendar-week">
<!-- <h2> week <?php echo $week; ?> <?php echo date("F Y", strtotime($begin)) ?> </h2>-->
<h2> week <?php echo date("F Y", strtotime($begin)) ?> </h2>
Previous |
Next
</span>
<div id="calendar">
<table>
<tr id="calendar-names"><!-- member names -->
<td> </td>
<?php foreach ($members as $member) { ?>
<td><p><?php echo $member ?></p></td>
<?php } ?>
</tr>
<?php
foreach ($posts as $key => $post) {
?>
<tr>
<td class="calendar-background-days"><p class="calendar-days"><?php echo date("D", strtotime($key)) ?></p></td>
<?php foreach ($members as $memberKey => $member) { ?>
<td class="calendar-background-content">
<?php foreach ($post as $contentKey => $content) { ?>
<?php if ($contentKey == $memberKey) { ?>
<?php foreach ($content as $c) { ?>
<p><?php echo $c['Post']['content'] ?></p>
<?php } ?>
<?php
}
}
?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
</div>
in model
public function getWeekPosts($begin, $end) {
$res = $this->find("all", array("order" => array('deadline', 'member_id'), "conditions" => "deadline BETWEEN '" . $begin . "' AND '" . $end . "'"));
$res = $this->formatWeekPosts($begin, $res);
return $res;
}
public function formatWeekPosts($begin, $res) {
$dt = array();
for ($i = 0; $i < 6; $i++) {
$dt[] = date("Y-m-d", strtotime($begin . "+ " . $i . " days"));
}
$members = array();
$pastMember = "";
foreach ($res as $r) {
if ($pastMember != $r['Post']['member_id']) {
$members[] = $r['Post']['member_id'];
$pastMember = $r['Post']['member_id'];
}
}
$posts = array();
foreach ($dt as $d) {
foreach ($res as $r) {
if ($r['Post']['deadline'] == $d) {
$posts[$d][] = $r;
}
}
}
$sortedPosts = array();
foreach ($members as $member) {
foreach ($posts as $key => $postDate) {
foreach ($postDate as $key2 => $p) {
if ($p["Post"]['member_id'] == $member) {
$sortedPosts[$key][$p["Post"]['member_id']][$key2] = $p;
}
}
}
}
ksort($sortedPosts);
return $sortedPosts;
}
public function PostgetUniqueMembers($posts) {
$members = array();
foreach ($posts as $post) {
foreach ($post as $p) {
$members[$p[key($p)]["Post"]["member_id"]] = $p[key($p)]["Member"]['username'];
}
}
return $members;
}

Related

View can't call Controller Function Zend Framework 2

I can't call my controller in view page. even if i use print_r in controller but it didn't show. I have body_product.phtml view code:
<table class="table table-hover table-condensed">
<tbody>
<?php
$i = 1;
//print_r("list produk:".$this->productList);
foreach ($this->productList as $data) {
$desc=explode(",",$data['descriptions']);
?>
<tr>
<th colspan="3">
<input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
<input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
<h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
</th>
</tr>
<tr id="dashe">
<td>
<ul class="myul">
<?php foreach($desc as $descriptions) { ?>
<li class="myli"> <?php echo $descriptions; ?></li>
<?php } ?>
</ul>
</td>
<td>
<h4 class="prize">
<?php setlocale(LC_MONETARY, 'id_ID');
echo money_format('%.2n', $data['package_price']); ?>
/ month
</h4>
</td>
<td>
<p id="btn-hrm" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
and in the indexController:
public function loadProductAction() {
$viewModel = new ViewModel();
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if ($data) {
foreach ($data as $val) {
array_push($arr, $val);
}
}
print_r('teaaat'.$arr);
$viewModel->setVariables(array('productList' => $arr))
->setTerminal(true);
return $viewModel;
}
If I open print_r in the view,it show error Warning: Invalid argument supplied for foreach() in.... I think it cause of view can't call the controller.
Help me please,thanks.
Firstly when you try to use print_r it is expecting an array. So it should be something like print_r($arr). Also give this a try and see if it helps.
public function loadProductAction() {
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if (is_array($data) && !empty($data)) {
foreach ($data as $val) {
array_push($arr, $val);
}
} else {
echo '$data is not an array or it is empty';
}
print_r($arr);
return new ViewModel(array(
'productList' => $arr
));
}

timetable for scheduled subjects

i got it right though my problem now is how can i put a style on the table row that has the data inside it and much better if i can put rowspan to it so that it will look like one table row.
here is the output:
and here is my code:
<?php
$db = new mysqli("localhost", "root", "", "bsu_db");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$times = ["7:00:00", "7:30:00", "8:00:00", "8:30:00", "9:00:00", "9:30:00", "10:00:00", "10:30:00", "11:00:00", "11:30:00", "12:00:00"];
$days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
$SubjDay = array();
$sqlSubjDay = "SELECT * FROM subject_day_tbl";
$qrySubjDay = $db->query($sqlSubjDay);
while($rowSubjDay = $qrySubjDay->fetch_assoc()){
//array_push($SubjDay, $rowSubjDay['sub_day'], $rowSubjDay['start_time'], $rowSubjDay['end_time']);
$SubjDay[] = $rowSubjDay;
}
json_encode($SubjDay);
?>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th></th>
<?php
foreach ($days as $day ) {
echo "<th>$day</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($times as $time) {
?>
<tr>
<td><?php echo $time; ?></td>
<?php
foreach ($days as $day) {
echo "<td>";
foreach($SubjDay as $sd){
if($day == $sd['sub_day'] && strtotime($time) >= strtotime($sd['start_time']) && strtotime($time) <= strtotime($sd['end_time'])){
echo $sd['sub_day'] , " " , $sd['start_time'] , " - " , $sd['end_time'];
}
}
echo "</td>";
}
?>
</tr>
<?php
}
?>
</tbody>
</table>

echo out image in mysql while loop not working?

i have a message type system set up and i am using a mysql while loop to echo out the contents from my table and am also trying to echo out the users profile image so that it appears next to each result being echoed. since this is a 2 way message system their is the photo of 'to_user_id'/recipient and 'from_user_id'/sender. this are coded as $photo and $photo2
its all working fine, the images are coming out as they should for each result being echoed by the while loop, however and for some reason, the first result is not echoing out the image, just the div container
does anyone know why this is. heres my code, thanks.
<div class="message_field">
<div class="inner_frame">
<?
$query_for_result=mysql_query("SELECT *
FROM ptb_messages
WHERE msg_id like '%".$query."%'");
$inbox_set = get_inbox();
while($data_fetch=mysql_fetch_array($query_for_result))
if (isset($_SESSION['user_id'])) {
if ($data_fetch['from_user_id'] == $_SESSION['user_id']){
{
$photo = "data/photos/{$data_fetch['from_user_id']}/_default.jpg";
if (!file_exists($photo)) {
$photo = "data/photos/0/_default.jpg";
}
$photo2 = "data/photos/{$data_fetch['to_user_id']}/_default.jpg";
if (!file_exists($photo2)) {
$photo2 = "data/photos/0/_default.jpg";
}
?>
<div class="message_bubble2">
<div class="msg_bubble_img"><img src="<?php echo $photo ?>" width="40" heigh="40" border="0"></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail2"></div><div class="message_bubble_tail2_shadow"></div><div class="message_bubble_tail2_shadow_bottom"></div><div class="message_bubble_tail2_outline"></div>
<div class="message_bubble_footer2">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set)) { ?>
<?php echo "".$review_from.""; ?> <?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
if($t > 0) break;
$i+=1;
}
echo "sent by you ".$t.$labels[$i]." ago"; } ?>
</div></div>
<?
}
}else{
if (isset($_SESSION['user_id'])) {
if ($data_fetch['from_user_id'] !== $_SESSION['user_id']){
{
?>
<div class="message_bubble"><div class="msg_bubble_img2"><img src="<?php echo $photo2 ?>" width="40" heigh="40" border="0"></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail"></div><div class="message_bubble_tail_shadow"></div><div class="message_bubble_tail_shadow_bottom"></div><div class="message_bubble_tail_outline"></div>
<div class="message_bubble_footer">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set)) { ?>
<?php echo "".$review_from.""; ?> <?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
if($t > 0) break;
$i+=1;
}
echo "sent from ".$inbox['from_user_id']." ".$t.$labels[$i]." ago"; } ?>
</div></div>
<? } } } } } ?>
Try forcing the order of the SELECT statement:-
<?
if (isset($_SESSION['user_id']))
{
$query_for_result=mysql_query("SELECT *, IF(from_user_id= '".mysql_real_escape_string($_SESSION['user_id'])."', 0, 1) AS ForceOrder
FROM ptb_messages
WHERE msg_id like '%".$query."%'
ORDER BY ForceOrder, ");
$inbox_set = get_inbox();
while($data_fetch=mysql_fetch_array($query_for_result))
{
if ($data_fetch['from_user_id'] == $_SESSION['user_id'])
{
{
$photo = "data/photos/{$data_fetch['from_user_id']}/_default.jpg";
if (!file_exists($photo))
{
$photo = "data/photos/0/_default.jpg";
}
$photo2 = "data/photos/{$data_fetch['to_user_id']}/_default.jpg";
if (!file_exists($photo2))
{
$photo2 = "data/photos/0/_default.jpg";
}
?>
<div class="message_bubble2">
<div class="msg_bubble_img"><img src="<?php echo $photo ?>" width="40" heigh="40" border="0"></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail2"></div><div class="message_bubble_tail2_shadow"></div><div class="message_bubble_tail2_shadow_bottom"></div><div class="message_bubble_tail2_outline"></div>
<div class="message_bubble_footer2">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set))
{ ?>
<?php echo "".$review_from.""; ?> <?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t)
{
if($t > 0) break;
$i+=1;
}
echo "sent by you ".$t.$labels[$i]." ago";
} ?>
</div></div>
<?
}
}
else
{
if (isset($_SESSION['user_id']))
{
if ($data_fetch['from_user_id'] !== $_SESSION['user_id'])
{
{
?>
<div class="message_bubble"><div class="msg_bubble_img2"><img src="<?php echo $photo2 ?>" width="40" heigh="40" border="0"></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail"></div><div class="message_bubble_tail_shadow"></div><div class="message_bubble_tail_shadow_bottom"></div><div class="message_bubble_tail_outline"></div>
<div class="message_bubble_footer">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set))
{ ?>
<?php echo "".$review_from.""; ?> <?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t)
{
if($t > 0) break;
$i+=1;
}
echo "sent from ".$inbox['from_user_id']." ".$t.$labels[$i]." ago";
} ?>
</div></div>
<?
}
}
}
}
}
}
?>

how to limit foreach loop to three loops

how to limit this loop ..just thee loops..thanks for helping
<?php
foreach($section['Article'] as $article) :
?>
<tr>
<td>
<?php
if ($article['status'] == 1) {
echo $article['title'];
}
?>
</td>
<td>
<?php
if($article['status']== 1) {
echo ' '.$html->link('View', '/articles/view/'.$article['id']);
}
?>
</td>
</tr>
<?php
endforeach;
?>
Slice the array.
foreach(array_slice($section['Article'], 0, 3) as $article ):
first, prepare your data
$i = 1;
$data = array();
foreach($section['Article'] as $article ) {
if($article['status']== 1) {
$article['link'] = $html->link('View', '/articles/view/'.$article['id']);
$data[] = $article;
if ($i++ == 3) break;
}
}
$section['Article'] = $data;
then display it
<?php foreach($section['Article'] as $article ): ?>
<tr>
<td><?php echo $article['title'] ?></td>
<td> <?php echo $article['link']?></td>
</tr>
<?php endforeach ?>
This will help if your array is numerically indexed
foreach($section['Article'] as $i => $article ):
if ($i > 3) break;
Otherwise - manually increment the counter:
$i = 0;
foreach($section['Article'] as $article ):
if ($i++ > 3) break;
It'd be easier to use a for() loop to do this, but to answer the question:
<?
$i = 0;
foreach ($section['Article'] AS $article):
if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>
A foreach loop wouldn't be the best if you need to limit it. Try using a for loop.
<?php
for(i=1; i<=3; i++)
{
$article = $section['Article'];
?>
<tr>
<td><?php if($article['status']== 1){echo $article['title'];} ?></td>
<td><?php if($article['status']== 1){echo ' '.$html->link('View', '/articles/view/'.$article['id']);}?></td>
</tr>
<?php } ?>
This code will make the text loop 3 times.
Awesome one must try this one
<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc'); foreach($pages as $page) {
$count++;
if ( $count < 50) { // only process 10 ?>
<div class="main_post_listing"> <?php echo $page->post_title ?><br /></div>
<?php
} } ?>

printing multi dimentional array

i have this multi dimentional array that i want to print into a table having each record/item go into its own row but it goes column wise. this is the output that im getting: http://mypetshopping.com/product.php
ps: the value of $product will by dynamic based on what product is being viewed.
<?php
session_start();
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hash</th>
<th>Quantity</th>
<th>Size</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<?php
function addCart($product, $quantity, $size,$color) {
$hash = md5($product);
$_SESSION['cart'][$product]['name'] = $product;
$_SESSION['cart'][$product]['hash'] = $hash;
$_SESSION['cart'][$product]['quantity'] = $quantity;
$_SESSION['cart'][$product]['size'] = $size;
$_SESSION['cart'][$product]['color'] = $color;
}
addCart('Red Dress',1,'XL','red');
addCart('Blue Dress',1,'XL','blue');
addCart('Slippers',1,'XL','orange');
addCart('Green Hat',1,'XXXL','green');
$cart = $_SESSION['cart'];
foreach($cart as $product => $array) {
foreach($array as $key => $value) {
?>
<tr>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
</tr>
<?php
}
}
?>
I think your looping code should be written as:
<?php foreach( $cart as $product => $array ) { ?>
<tr>
<?php foreach( $array as $key => $value ) { ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php } ?>
try altering your code to:
foreach($cart as $product => $array) { ?>
<tr>
<?php
foreach($array as $key => $value) {
?>
<td><?=$value;?></td>
<?php
}
?>
</tr>
<?php
}
<?php
echo "<ul>";
for ( $layer = 0; $layer < 3; $layer++ )
{
echo "<li>The layer number $layer";
echo "<ul>";
for ( $row = 0; $row < 3; $row++ )
{
echo "<li>The row number $row";
echo "<ul>";
for ( $col = 0; $col < 3; $col++ )
{
echo "<li>".$shop[$layer][$row][$col]."</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
?>
http://www.webcheatsheet.com/php/multidimensional_arrays.php

Categories