theme_table drupal development - php

update
I have made this table: I have 2 array's UGentArray and InternshipsArray. How can I set these in my table? instead of "Row column 1 data" and "Row column 2 data". My question is: I want the values from UgentArray and InternshipArray as 2 columns under Each title UGentID and Internships
enter code here// Make table and display on screen.
$tbl_header = array();
$tbl_header[] = array("data" => "UGentID");
$tbl_header[] = array("data" => "Internships");
$tbl_row = array();
foreach($studentUGentID as $key => $value) {
for($i = 0; $i<count($value); $i++) {
$tbl_row[] = $value[$i]['value'];
}
}
$tbl_row[] = "Row column 2 data";
$tbl_rows = array();
$tbl_rows[] = $tbl_row;
$html = theme_table($tbl_header,$tbl_rows);
return $html;![enter image description here][1]

Without having an example of the data you are starting with it is difficult to give you exact code to solve your problem.
In general the best way to built tables in Drupal is like this:
$table_headers = array(
t('UGentID'), // this is the header for the first column
t('Internships'), // this is the header for the second column
);
$table_rows = array()
foreach ($studentUGentID as $key => $value) {
$table_rows[] = array(
'column 1 data', // add all the data for the row one column
'column 2 data', // at a time
);
}
$html = theme('table', $table_headers, $table_rows);
As I said without knowing what is in $studentUGentID and $internships I cannot help further.
With sample data I could help you more.

Related

how to insert post in codeigniter

i want to ask. I made an automatic code that I will post to the database, but I have problems, the data does not enter the database.
here I make an automatic code for item code
$data['awb'] = $this->M_order->bikin_kode();
I want to enter the value into 'tracking_number'
$data['awb'] = $this->M_order->bikin_kode();
$resinya = $data['awb'];
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = array();
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data,
array(
'tracking_number' => $resinya['awb'],
)
);
}
$numrow++;
}
$this->M_order->insert_multiple($data);
when I insert the data it doesn't enter
I think you have passing data wrongly to array. Use $data['awb'] instead of $resinya['awb'] for 'tracking_number'.like this
array_push($data,
array(
'tracking_number' => $data['awb'],
)
);
Now, your code insert the data outside the foreach. so it will execute the last data into the database table.

separate column by id using foreach

I have a score table, and I will separate the column by id
my table is,
and I hope the output table will
I don't know the logic if I use foreach in CodeIgniter, thanks
and have tried this
foreach ($data as $row) {
$records["list"][] = array(
$row['ID_STUDENT'],
$row['ID_TYPE'],
$row['SCORE'],
);
}
This will build you the array you are looking for, although I would suggest that you should write a query to retrieve your data in the format you are looking for.
$studentsScores = [
['idType'=>1, 'idStudent'=>1, 'score'=>'80'],
['idType'=>1, 'idStudent'=>2, 'score'=>'60'],
['idType'=>2, 'idStudent'=>3, 'score'=>'70'],
['idType'=>1, 'idStudent'=>4, 'score'=>'80'],
['idType'=>3, 'idStudent'=>1, 'score'=>'90'],
['idType'=>4, 'idStudent'=>2, 'score'=>'100'],
];
// Columns with default values
$columns = array_fill_keys(array_unique(array_column($studentsScores, 'idType')), 0);
$res = [];
foreach($studentsScores as $record){
if(!isset($res[$record['idStudent']])) $res[$record['idStudent']] = $columns;
$res[$record['idStudent']][$record['idType']] = $record['score'];
}
print_r($res);
Hope this helps,

create ad multidimensional array and display it with a foreach loop

I've a problem with php and foreach...
The first query result like this:
while ($row = $s->fetch())
{
$registration[] = array(
'id_registration' => $row['id_registration'],
'discipline' => $row['discipline'],
'speciality' => $row['speciality'],
'category' => $row['category'],
'subcat' => $row['subcat']
);
}
excuse me, I was not very precise...
I have 2 table
- the first has a primary key (id_registration) that identifies the registration
- in the second table there are firstrname and lastname of the athletes which refer to the first table by id_registration.
how can I get all the registrations and the athletes of every registration and print all with one o more foreach loop?
I hope I was clear.
$iscrizioni = array();
while ($row = $s->fetch())
{
$iscrizioni['id_gara'] = $row['id_gara'];
$iscrizioni['disc'] = $row['disc'];
$iscrizioni['spec'] = $row['spec'];
$iscrizioni['cat'] = $row['cat'];
$iscrizioni['subcat'] = $row['subcat'];
}
pre($iscrizioni);

(PHP+MySQL) How can I echo the column name on a specific condition?

I am using PHP 5.4 with a MySQL database.
This database represents a media library. The table I'm dealing with has one column, "Title", with obvious contents, and then a series of boolean columns, representing the availability of that title on a given platform. So a row looks like
TITLE: "Curb Your Enthusiasm: The Game"
PS4: 0
Atari 2600: 1
Dreamcast: 0
And so on.
The PHP code I would like to write be, in pseudocode,
Echo row[0] (title)
Cycle through other cells in the row
If the cell is '0' or NULL, do nothing
But if the cell is '1', echo the name of that column
So the result would be the echoing of
Curb Your Enthusiasm: The Game (Atari 2600, WonderSwan, Saturn)
It's the fourth statement that I can't quite work out. It seems to require the function mysqli_fetch_field, but I'm not sure of the syntax, and nothing I try after googling quite works.
I'd really appreciate any advice or examples someone could offer!
$database = mysqli_connect(SERVER,USERNAME,PASSWORD,'games');
$query = mysqli_query($database,"SELECT * FROM games` WHERE NAME LIKE '%ZELDA%'");
while ($row = mysqli_fetch_row($query)) {
echo $row[0]; // Echo title
for ($i=0;$i<sizeof($row);$i++) {
if ($row[$i] === '1') {
// ???????
}
}
}
Here is some rough untested code that should hopefully get you going.
while ($row = mysqli_fetch_assoc($query)) {
$columns = array(); // this will track the additional columns we need to display
foreach($row AS $column => $value) {
if($column == "title") {
echo $value; // this is the title, just spit it out
continue;
}
if($value == 1) {
// We have a column to display!
$columns[] = $column;
}
}
if(count($columns)) {
// We have one or more column names to display
echo " (" . implode(", ",$columns) . ")";
}
}
Some things to point out:
Using mysqli_fetch_assoc will allow you access to column names along with the values, which is useful here.
Keep track of the columns you want to display in an array first, this makes it easier at the end of each loop to format the output.
Sounds like you can do something like this:
// Simulates DB fetch
$titles = array(
array(
'TITLE'=>'Curb Your Enthusiasm: The Game',
'PS4'=>0,
'Atari 2600'=>1,
'Dreamcast'=>0
),
array(
'TITLE'=>'Curb Your Enthusiasm: The Book',
'PS4'=>1,
'Atari 2600'=>1,
'Dreamcast'=>0
)
);
foreach($titles as $title){
// get supported platforms
$supportedPlatforms = array();
foreach($title as $titleAttribute=>$titleValue){
if($titleAttribute != 'TITLE' && $titleValue == 1)
$supportedPlatforms[] = $titleAttribute;
}
echo $title['TITLE'] . ' (' . implode(', ', $supportedPlatforms) . ')' . "<br>";
}
Try running it here: http://phpfiddle.org/lite/code/pr6-fwt

json DataTable in googlecharts

I am having a bit of trouble getting the proper format of json string.
I have a database table that looks something like this:
Table Columns: emp month sales
Table rows: Bob 1 100
Bob 2 150
Jane 1 125
Jane 2 130
Mary 1 110
Mary 2 130
Within drawChart(), I can create something like this statically:
var data = google.visualization.arrayToDataTable([
['Month', 'Bob', 'Jane', 'Mary],
['Jan', 100, 125, 110],
['Feb', 150, 130, 130]
]);
In the end, the json string needs to look like this:
{"cols":[{"label":"Month","type":"string"},
{"label":"Bob","type":"number"},
{"label":"Jane","type":"number"},
{"label":"Mary","type":"number"}],
"rows":[{"c":[{"v":100},{"v":125},{"v":110}]},
{"c":[{"v":150},{"v":130},{"v":130}]}]}
But I am having trouble pulling from the table to come up with proper json formatting that is equivalent to the above. I am following the steps from here... PHP MySQL Google Chart JSON - Complete Example
But that example is only for a single data set. if you were to add multiple weeks instead of having just one data set, how do run the query?
To get your data in the format you want, you have to pivot your data. Some databases support pivotting, but others like MySQL don't. If you are stuck without pivot support, then you have to resort to trickery to make it happen. Here's one way you could do it:
SELECT
month,
SUM(if(employee = "Bob", sales, 0)) AS Bob,
SUM(if(employee = "Jane", sales, 0)) AS Jane,
SUM(if(employee = "Mary", sales, 0)) AS Mary
FROM myTable
GROUP BY month
This requires that you know ahead of time what the employee names are so that you can write the SQL statement (either when you write the code, or you could pull them from another SQL query and write a dynamic SQL query).
Asgallent, thank you. Your response gave me the direction I needed. I was able to do it all dynamically via SQL. I made two queries: 1 to the "saleperson" table to get the names, and then another to pivot the data as you suggested. For anyone else that might find this helpful, here is the code I have.
The queries (Note: I am using codeigniter):
$sp_qry = $this->db->query('select * from salespeople');
$qryString="";
foreach ($sp_qry->result_array() as $row)
{
$qryString.= ",SUM( IF( `salespeople_id` =" . $row['salespeople_id'] . ", `num_sold` , 0 ) ) AS " . $row['name'];
}
$qry= "SELECT `month` " . $qryString . " FROM `product_sales`
GROUP BY `month`";
$query = $this->db->query($qry);
return $query->result_array();
and in my viewing page
$rows = array();
$table = array();
$cols = array();
$cols[] = array('label' => 'Month', 'type' => 'string');
foreach ($salespeople as $sp)
{
$cols[] = array('label' => $sp['name'], 'type' => 'number');
}
$table['cols'] = $cols;
foreach ($sales as $chart_item)
{
$tmp=array();
$tmp[] = array('v' => (string) $chart_item['month']);
foreach ($salespeople as $sp)
{
$name=$sp['name'];
$tmp[] = array('v' => (int) $chart_item[$name]);
}
$rows[] = array('c' => $tmp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);

Categories