How to get the array values which the array created in function? - php

When I think about the functional of array and I tried to create an array and store in the function acts as the clock but how can I get the array values outside the function?
function theClock($a,$b,$c){
$time['Hour'] = $a;
$time['Minute'] = $b;
$time['Seconds'] = $c;
return $time;
}
//How can I call the array values in there that $time is stored?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Sorry, I forgot to mention that I would like to use foreach() to shows the array values as the table form.
$show1 = '<table border ="1" >';
foreach($time as $ck => $tk){
$show1 .= '<tr><td>' . $ck . '</td><td>' . $tk . '</td></tr>';
}
$show1 .= '</table>';
$show2 = '<table border ="1">';
foreach($time as $tk){
$show2 .= '<td>' . $tk . '</td>';
$show2 .= '<td>:</td>';
}
$show2 .= '</table>';
echo $show1;
echo $show2;
theClock(11,12,13);

function clock($a,$b,$c){
$time['Hour'] = $a;
$time['Minute'] = $b;
$time['Seconds'] = $c;
return $time;
}
$time = clock(12, 30, 00);
$hour = $time['Hour'];
$minute = $time['Minute'];
$seconds = $time['Seconds'];
print $hour; // 12
print $minute; // 30
print $seconds; // 00
As per your edit, to use in a foreach loop:
$html = '<table border ="1" >';
foreach($time as $unit => $value){
$html .= "<tr><td>$unit:</td><td>$value</td></tr>";
}
$html .= '</table>';

You can do this by just calling your function with your parameters like
$time = clock(10,20,30);

try this
$vararr=clock($a,$b,$c);

$time = clock(12, 40, 15);
echo "<pre>";print_r($time);

Related

MySQL PHP Query in CI

I need help with this. I must pass a PHP MySQL function to CodeIgniter, but it does not show me the data, it stops right in if(array_key_exists($pos, $rs))
Does the query well but does not enter the conditional if
Any solution?
This is my Code in CodeIgniter
public function table($hour, $row)
{
global $rs;
if ($rs === null)
{
$this->db->select("CONCAT(t.tbl_row, '_', t.tbl_col) as pos, t.tbl_id, t.sub_id, s.sub_name", false);
$this->db->join('redips_timetable AS t', 't.sub_id = s.sub_id');
$rs = $this->db->get('redips_subject AS s')->result();
}
echo '<tr>';
echo '<td class="mark dark">' . $hour . '</td>';
for ($col = 1; $col <= 5; $col++)
{
echo '<td>';
$pos = $row . '_' . $col;
if(array_key_exists($pos, $rs))
{
$elements = $rs[$pos];
$id = $elements['sub_id'] . 'b' . $elements['tbl_id'];
$name = $elements['sub_name'];
$class = substr($id, 0, 2);
echo "<div id=\"$id\" class=\"redips-drag $class\">$name</div>";
}
echo '</td>';
}
echo "</tr>\n";
}
Original MySQL code
function table($hour, $row) {
global $rs;
// if $rs is null then query database (this should be executed only once - first time)
if ($rs === null)
{
// first column of the query is used as key in returned array
$rs = sqlQuery("select concat(t.tbl_row,'_',t.tbl_col) as pos, t.tbl_id, t.sub_id, s.sub_name
from redips_timetable t, redips_subject s
where t.sub_id = s.sub_id", 0);
}
print '<tr>';
print '<td class="mark dark">' . $hour . '</td>';
// column loop starts from 1 because column 0 is for hours
for ($col = 1; $col <= 5; $col++) {
// create table cell
print '<td>';
// prepare position key in the same way as the array key looks
$pos = $row . '_' . $col;
// if content for the current table cell exists
if (array_key_exists($pos, $rs)) {
// prepare elements for defined position (it can be more than one element per table cell)
$elements = $rs[$pos];
// id of DIV element will start with sub_id and followed with 'b' (because cloned elements on the page have 'c') and with tbl_id
// this way content from the database will not be in collision with new content dragged from the left table and each id stays unique
$id = $elements[2] . 'b' . $elements[1];
$name = $elements[3];
$class = substr($id, 0, 2); // class name is only first 2 letters from ID
print "<div id=\"$id\" class=\"redips-drag $class\">$name</div>";
}
// close table cell
print '</td>';
}
print "</tr>\n";
}
/*
if you need find array key
you got object in $rs varible
( $rs = $this->db->get('redips_subject AS s')->result(); )
so need convert obj to array in for each loop
*/
public function table($hour, $row)
{
global $rs;
if ($rs === null)
{
$this->db->select("CONCAT(t.tbl_row, '_', t.tbl_col) as pos, t.tbl_id, t.sub_id, s.sub_name", false);
$this->db->join('redips_timetable AS t', 't.sub_id = s.sub_id');
$rs = $this->db->get('redips_subject AS s')->result();
}
echo '<tr>';
echo '<td class="mark dark">' . $hour . '</td>';
for ($col = 1; $col <= 5; $col++)
{
$arr = get_object_vars($rs[$col]);
echo '<td>';
$pos = $row . '_' . $col;
if(array_key_exists($pos, $arr))
{
$elements = $arr[$pos];
$id = $elements['sub_id'] . 'b' . $elements['tbl_id'];
$name = $elements['sub_name'];
$class = substr($id, 0, 2);
echo "<div id=\"$id\" class=\"redips-drag $class\">$name</div>";
}
echo '</td>';
}
echo "</tr>\n";
}

Foreach Loop only uses last value [duplicate]

This question already has answers here:
How to store values from foreach loop into an array?
(9 answers)
Closed 1 year ago.
Hi I am using the Calendar Code u can find down in the code. Now I am using Sql to get some days I want to mark in the calendar. In the foreach loop u can see me using the array $tage.
When I print_r($tage) it gives me following Information: Array ( [0] => 9 [1] => 8 [2] => 8 [3] => 8 [4] => 8 [5] => 11 )
Maybe there is a Problem with the returning values, but using only the unique values was not possible either.
Fyi the only highlight I always get is the last value 11.
I think the whole issue comes down to the combination of while foreach and elseif. I can't find any Solution to this. Maybe someone figures it out.
Thanks :)
<?php
error_reporting();
ini_set('display_errors', 1);
setlocale(LC_TIME, "de_DE");
class Calendar{
private $month;
private $year;
private $daysofweek;
private $numdays;
private $date_info;
private $day_of_week;
private $tnr;
public function __construct($month,$year,$days_of_week =array('So','Mo','Di','Mi','Do','Fr','Sa')){
$this->month =$month;
$this->year =$year;
$this->days_of_week =$days_of_week;
$this->num_days =cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year);
$this->date_info =getdate(strtotime('first day of', mktime(0,0,0,$this->month,1,$this->year)));
$this->day_of_week =$this->date_info['wday'];
$this->monat = date("n", mktime(0, 0, 0, $this->month, 1, $this->year));
}
public function show($tnr) {
$monate = array(1=>"Januar",
2=>"Februar",
3=>"März",
4=>"April",
5=>"Mai",
6=>"Juni",
7=>"Juli",
8=>"August",
9=>"September",
10=>"Oktober",
11=>"November",
12=>"Dezember");
include '../include/myt.php';
// WERT 0 entfernen
// GRÖße des ARRAY
$size =sizeof($tnr);
$now_mon= $this->monat;
foreach($tnr as $tnrs){
$sql= "SELECT * FROM termin WHERE nr = '$tnrs' AND monat ='$now_mon' ";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);
$tagdata = $row['tag'];
if($tagdata == ""){
"";
} else {
$tage[] = $tagdata;
}
}
$output= '<table id="table'. $this->monat . '" class="table table-striped">';
$output .= '<thead id="head'. $this->monat . '" style="text-align:center">'. $monate[$this->monat] . ' ' . $this->year . '</thead>';
$output .= '<tr>';
foreach( $this->days_of_week as $day)
{
$output .= '<th class="header center">' . $day . '</th>';
}
$output .= '</tr><tr>';
if($this->day_of_week > 0){
$output .= '<td colspan="' . $this->day_of_week . '"></td>';
}
$current_day =1;
while ( $current_day <= $this->num_days){
if($this->day_of_week ==7){
$this ->day_of_week =0;
$output .= '</tr></tr>';
}
///PROBLEM
print_r($tage);
foreach($tage as $tag){
if($current_day == $tag){
$current='style ="background: black;"';
} else {
$current= '';
}
}
///PROBLEM
$output .='<td class="day"'.$current.'>' . $current_day . '</td>';
$current_day++;
$this->day_of_week++;
}
if($this->day_of_week != 7){
$remaining_days = 7 - $this->day_of_week;
$output .= '<td colspan="' . $remaining_days . '"></td>';
}
$output .= '</tr>';
$output .= '</table>';
echo $output;
}
}
?>
Change your foreach loop as below:
foreach($tnr as $tnrs) {
$sql= "SELECT * FROM termin WHERE nr = '$tnrs' AND monat ='$now_mon' ";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);
$tagdata = $row['tag'];
if(!empty($tagdata)) {
$tage[] = $tagdata;
}
}
Can you please check at line $output .='<td class="day"'.$current.'>' . $current_day . '</td>';
It should be $output .='<td class="day"'.$current.'">' . $current_day . '</td>';, You are forgot to add ".
May be it is help you.

Sort a PHP Array table by date

I have a multi-columns HTML table generated by PHP with Array, taking data from a table in a database which contains a list of entries. One of the 5 columns is a datestamp. I would like the HTML table to be sorted by timestamp, without any code it sorts it by ID (column0).
Here is the code I have to sort:
$table .= "<tr><td>" . $column0[$i][0] ."</td><td>" . $column1[$i][1] . "</td><td>" . $column2[$i][2] . "</td><td>" . $column3[$i][3] . "</td><td>" . $column4[$i][4] . "</td><td>" . $column5[$i][5] . "</td></tr>";
$column5[$i][5] is the one containing the datestamp. I've tried sort(), asort(), array_multisort()... without any luck.
Here is the SQL table structure:
column0: id
column1: number1
column2: text1
column3: number2
column4: text2
column5: date (format: Y-m-d H:m:s)
And here is an example of its content, for which I need to sort by the column date:
id.....number1.....text1.....number2.....text2................date
1........75.............toto..........58...........tata.......2014-04-07 16:43:51
2........34.............tutu..........07...........titi.........2013-04-09 08:27:34
3........83.............tyty..........53...........tete.......2015-04-08 12:36:18
Thank you!
You can use usort() and compare date by strtotime(). An example here..
$arr = array(
0 => array('id'=>1,'number1'=>'75','text1'=>'toto','number2'=>'58','text2'=>'tata','date'=>'2014-04-07 16:43:51',),
1 => array('id'=>2,'number1'=>'34','text1'=>'tutu','number2'=>'07','text2'=>'titi','date'=>'2013-04-09 08:27:34',),
2 => array('id'=>3,'number1'=>'83','text1'=>'tyty','number2'=>'53','text2'=>'tete','date'=>'2015-04-08 12:36:18',),
);
function sort_by_date($a, $b) {
$a = strtotime($a['date']);
$b = strtotime($b['date']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($arr, 'sort_by_date');
$keys = array_keys(current($arr));
$html = '<table border="1"><tr>';
foreach($keys as $key){
$html .= '<th>'.$key.'</th>';
}
$html .= '</tr>';
foreach($arr as $value){
$html .= '<tr>';
foreach($value as $val){
$html .= '<td>'.$val.'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
Output:

get element from an array as a field name in PHP

I have several fields from a mysql table (width,diameter etc). Instead of building up the strings from returned data one by one
$rows = $wpdb->last_result; // Wordpress version
foreach(array_chunk($rows, 5) as $i => $pair)
{
$width .= "<tr><td>Width</td>";
$diameter .= "<tr><td>Diameter</td>";
foreach($pair as $row)
{
$width .= "<td>$row->width mm</td>";
$diameter .= "<td>$row->diameter mm</td>";
}
$width .= "</tr>";
$diameter .= "</tr>";
}
Can I turn thickness,diameter,width into an array and loop over them like this:
foreach(array_chunk($rows, 5) as $i => $pair)
{
$measures = array("width"=>"Width","diameter"=>"Diameter","thickness"=>"Thickness","hours"=>"Hours");
foreach($measures as $measure=>$title)
{
$measure .= "<td>$title</td>";
}
foreach($pair as $row)
{
foreach($measures as $measure=>$title)
{
$measure .= "<td>$row->".$measure." mm </td>";
}
}
foreach($measures as $measure=>$title)
{
$measure .= "</tr>";
}
}
But I'm getting Object of class stdClass could not be converted to string error, so is it possible to do that?
This is not allowed:
"<td>$row->".$measure." mm </td>";
I think the concatenation is messing it up, try like this:
"<td>".$row->$measure." mm </td>";
This should be helpful.

Building a Table of Data with an Array of Arrays

I have an Array of Arrays and Want to create a Tabular data layout. This is not the exact code, as how their generated is quite the cluster (Coming from COBOL interaction) but this should give me enough to make it work if someone can think of a clean way to code this.
array(
Array(847.0, 97010, 11)
Array(847.0, 97012, 10)
Array(847.1, 97010, 08)
Array(847.1, 97012, 14)
)
So I want to put these into a Table that looks something like
97010 97012
847.0 11 10
847.1 08 14
The first 2 elements of the arrays will always be the two axis and the 3rd the contents of the table.
Any Suggestions? thanks!
$table = array();
$columns = array();
// copy the array (x, y, value) into a table
// keeping track of the unique column names as we go
foreach ($dataSet as $point) {
// provided sample data used floats, ensure it is a string
$x = strval($point[0]);
$y = strval($point[1]);
$data = $point[2];
if (!isset($table[$x])) {
$table[$x] = array();
}
$table[$x][$y] = $data;
// quick and dirty style 'unique on insert'
$columns[$y] = true;
}
// switch the column names from title => true to just titles
$columns = array_flip($columns);
// Display the table
echo '<table>';
// Header row
echo '<tr>';
echo '<th> </th>';
foreach ($columns as $columnTitle) {
echo '<th>' . $columnTitle . '</th>';
}
echo '</tr>';
// Bulk of the table
foreach ($table as $rowTitle => $row) {
echo '<tr>';
echo '<th>' . $rowTitle . '</th>';
foreach ($columns as $columnTitle => $junk) {
if (isset($row[$columnTitle]) {
echo '<td>' . $row[$columnTitle] . '</td>';
} else {
// Handle sparse tables gracefully.
echo '<td> </td>';
}
}
echo '</tr>';
}
echo '</table>';
From what I understood:
$arr[847.0][97010] = '11';
$arr[847.0][97012] = '10';
$arr[847.1][97010] = '08';
$arr[847.1][97012] = '14';
And you may create a table:
$result = "<table>";
foreach($arr as $row_key => $row) {
$result .= "<tr>";
foreach($row as $column_key => $data) {
$result .= "<td>".$data."</td>";
}
$result .= "</tr>";
}
$result .= "</table>";
echo $result;

Categories