I am trying to manipulate my table using rowspan but I'm kinda struggling.
My expected output should be if there's two duplicated names I want it to have a rowspan in the table. But my problem occurs if there's a third duplicate and its separated between another name (in this case 'ruby' is in between) my table will have an excess column.
My code is kinda like this
sample:
$gems = array(
0 => array:2[
name: Amber
value: 20
]
1 => array:2[
name: Amber
value: 30
]
2 => array:2[
name: Ruby
value: 40
]
3 => array:2[
name: Amber
value: 50
]
4 => array:2[
name: Emerald
value: 60
]
);
This is how I map the rowspan
$rows = array();
foreach($rows as $key => $gem){
$rows[$key] = $gem['name'];
}
$arr = array_replace($row,array_fill_keys(array_keys($row, null),''));
$rowspan = array_count_values($ar);
$rowspan output will be this which i use on the table.
array:3(
"Amber" => 3
"Ruby" => 1
"Emerald" => 1
)
Then my display is kinda like this (not exact). Im injecting the rowpan on $rowspanDuplicated
$html = '<table>'
foreach($gems as $key => $gem){
$html .= '<tr>'
$rowspanDuplicated = 'rowspan="'. $rowspan[$gem['name']].'"';
if($rowspan[$gem['name']] <= 1){
$rowcount = 1;
$html .= '<td>' . $this->n($sv['name']). ' </td>'
} else {
if($rowcount<=1){
$html .= '<td' . $rowspanDuplicated . '>' .$this->n($sv['name']) . '</td>';
}
if($rowspan[$sv['name']] == $rowcount){
$rowcount=1;
} else {
$rowcount++;
}
}
$html .= '<td>' . $this->n($sv['value']). ' </td>'
$html .= '</tr>'
}
$html = '</table>'
The problem to this code is that $gem[3] will also have a rowspan
I want my table something like this.
_________________
Amber | 20
|-----
| 30
________|________
Ruby | 40
_________________
Amber | 50
________|________
Emerald | 60
_________________
I think you should group your array based on name in the first place.
public function group($gems)
{
$grouppedGems = [];
foreach($gems as $gem) {
if (!array_key_exists($gem['name'], $grouppedGems))
$grouppedGems[$gem['name']] = [];
$grouppedGems[$gem['name']][] = $gem;
}
return $grouppedGems;
}
$html = '<table>'
foreach($gems as $name => $gem){
$html .= '<tr><td rowspan="' . count($gem) . '">' . $name . '</td><td>';
foreach($gem as $item) {
$html .="<tr><td>{$this->n($item['value'])}</td></tr>";
}
$html .= '</td></tr>';
}
$html = '</table>';
I didn't test the html string, but I think it will work. BTW, you won't need rowspan, bu I added it just in case.
blade version
#foreach($gems as $gem)
<tr>
<td
#if(!$loop->last && $gems[$loop->index+1]->gem['name'] == $gem['name']) rowspan=2 >{{$gem['name']}}</td>
<td>{{$gem['value']</td>
<tr>
#endforeach
Related
Here my url address for https://goalserve.com/getfeed/mytoken/topscorers/1204?json=1
How can I get the "player" data, I want to use make "top scorers standing widget" on my PHP Wordpress site.
{
"?xml":{
"#version":"1.0",
"#encoding":"utf-8"
},
"topscorers":{
"#sport":"soccer",
"tournament":{
"#name":"Premier League",
"#stage_id":"12041081",
"#gid":"1204",
"#is_current":"True",
"#id":"1204",
"player":[
{
"#pos":"1",
"#name":"Mohamed Salah",
"#team":"Liverpool",
"#team_id":"9249",
"#goals":"15",
"#penalty_goals":"2",
"#id":"138653"
},
{
"#pos":"2",
"#name":"Diogo Jota",
"#team":"Liverpool",
"#team_id":"9249",
"#goals":"10",
"#penalty_goals":"0",
"#id":"374031"
},
{
"#pos":"3",
"#name":"J. Vardy",
"#team":"Leicester City",
"#team_id":"9240",
"#goals":"9",
"#penalty_goals":"0",
"#id":"159732"
}
]
}
}
}
Answer
https://www.php.net/manual/en/function.json-decode.php
To do this you must first decode the json string into a php object. Then you must drill down to the proper data that you want. You can do this with the -> operator like so:
$players = json_decode($raw_string)->{'topscorers'}->{'tournament'}->{'player'};
Note that you should save the result of the decoded json string if you plan on reusing the data. Don't decode it more than once.
After you have the players, you can then iterate over the data and do what you want with it. Here is a minimal example with your data so you can see it all working together.
Example
Dataset
<?php
$raw_string = '
{
"?xml":{
"#version":"1.0",
"#encoding":"utf-8"
},
"topscorers":{
"#sport":"soccer",
"tournament":{
"#name":"Premier League",
"#stage_id":"12041081",
"#gid":"1204",
"#is_current":"True",
"#id":"1204",
"player":[
{
"#pos":"1",
"#name":"Mohamed Salah",
"#team":"Liverpool",
"#team_id":"9249",
"#goals":"15",
"#penalty_goals":"2",
"#id":"138653"
},
{
"#pos":"2",
"#name":"Diogo Jota",
"#team":"Liverpool",
"#team_id":"9249",
"#goals":"10",
"#penalty_goals":"0",
"#id":"374031"
},
{
"#pos":"3",
"#name":"J. Vardy",
"#team":"Leicester City",
"#team_id":"9240",
"#goals":"9",
"#penalty_goals":"0",
"#id":"159732"
}
]
}
}
}';
?>
Processing
<?php
$php_object = json_decode($raw_string);
$players = $php_object->{'topscorers'}->{'tournament'}->{'player'};
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>Pos</td>';
$html .= '<td>Player</td>';
$html .= '<td>Team</td>';
$html .= '<td>Goals</td>';
$html .= '</tr>';
foreach ($players as $p) {
$html .= '<tr>';
$html .= '<td>' . $p->{'#pos'} . '</td>';
$html .= '<td>' . $p->{'#name'} . '</td>';
$html .= '<td>' . $p->{'#team'} . '</td>';
$html .= '<td>' . $p->{'#goals'} . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo($html);
?>
Output
<table><tr><td>Pos</td><td>Player</td><td>Team</td><td>Goals</td></tr><tr><td>1</td><td>Mohamed Salah</td><td>Liverpool</td><td>15</td></tr><tr><td>2</td><td>Diogo Jota</td><td>Liverpool</td><td>10</td></tr><tr><td>3</td><td>J. Vardy</td><td>Leicester City</td><td>9</td></tr></table>
Pos
Player
Team
Goals
1
Mohamed Salah
Liverpool
15
2
Diogo Jota
Liverpool
10
3
J. Vardy
Leicester City
9
So, I have a function that's meant to take a multi-dimensional array and print it out as a nice html drill-down navigation:
<?php
function html_print_r($array = [], $level=0, $pass_id = "-000"){
$my_rand = rand();
if (is_array($array)){
foreach ($array as $key => $val){
echo '<p>';
for ($i = 0; $i < $level; $i++){
// Some visual effect, adding pipe symbols to show depth.
echo "|";
}
echo '<a onclick=$' . "('#" . $key . $my_rand . "').toggle()>" . $key . "</a> [+]";
html_print_r($val, true, $level + 1, $key . $my_rand);
echo "</p>";
}
} elseif(is_object($array)) {
html_print_r((array)$array, true, $level);
} else{
echo "<pre style='display:none; id='" . $pass_id . "' >" . $array . "</pre>";
}
}
The problem is if I pass it an array like
["Item1"=> ["Item1a"=>"foo"], "Item2" => "Bar"]
Instead of getting a nested result like
"Item1" [+]
| "Item1a" [+]
| | "foo"
"Item2"[+]
| "Bar"
I'm getting an incorrect printout more flat like
"Item1" [+]
""
|
"Item1z" [+]
""
|
"foo"
|
"Item2" [+]
""
|
"Bar"
I've looked over it a few times, and I can't quite figure out why. I feel like I'm missing something obvious.
Here what I got in view
I want to show it as Separate divs based on vendor name
How can i Group the vandor names and Show in different divs ?
I don't know how the data is being stored/retrieved but here is how I might do such sorting - assuming the variable $rows is set contains an associative array of all rows (similar to the results of a basic database query) and assuming "vendor" is the column name for the vendors (TSZ, TSR, etc), "sku" is the name of the SKU column, and "store_price" is the name of the store price column:
<?php
$vendorData = [];
foreach( $rows as $row ) {
if( !isset($vendorData[$row['vendor']]) ) {
$vendorData[$row['vendor']] = '';
}
$vendorData[$row['vendor']] .= '<div class="sku">' .
$row['sku'] . '</div>' .
'<div class="price">' . $row['store_price'] . '</div>';
}
foreach( $vendorData as $vendorName => $vendorHTML ) {
echo '<div id="' . $vendorName . '" class="vendor">' .
'<h3 class="name">' . $vendorName . '</h3>' .
'<div class="data">' . $vendorHTML . '</div></div>';
}
?>
Here is thing what you need:-
$newDataRow = []
foreach($dataRows as $dataItemRow):
$newDataRow[$dataRows['vendor']][] = $dataItemRow;
endforeach;
$op = '';
foreach($newDataRow as $vandorName => $vandorItemData):
$op .= '<div class="vendor_item_group">';
$op .= '<div class="vendor_name">'.$vandorName.'</div>';
$op .= '<div class="item_group">';
foreach($vandorItemData as $record):
$op .= '<span>'.$record['sku'].'</span>';
$op .= '<span>'.$record['price'].'</span>';
endforeach;
$op .='</div>';
$op .= '</div>';
endforeach;
echo $op;
Output Should be like this:-
TSZ TSR
TSZ K377 TSR319
2300 2250
TSR319
2300
This question already has answers here:
creating variable name by concatenating strings in php
(4 answers)
Closed 8 years ago.
Simple question, with im sure a simple answer, I just cant get it working!!
I have a function which will be passed one of 5 id's (for example "first", "second", ",third", "fourth" and "fifth").
Inside this function I want to refer to an array whose name is the id followed by _array (for example "first_array", "second_array" etc...)
How do I concatenate the id name passed to the function with the string "_array" ? I know how to do this in a string but not when referring to another variable!
To sum up i will have:
$i_d = "first" //passed to my function
$string = "_array"
and I want to link to an array called:
$first_array
EDIT
My code is the following:
$option_timescale = array ( "1"=>"Immediately",
"2"=>"1 Month",
"3"=>"2 Months",
"4"=>"3 Months",
"5"=>"4 Months",
"6"=>"5 Months",
"7"=>"6 Months",
"8"=>"No Timescale"
);
$option_bus_route = array ( "1"=>"1 minute walk",
"2"=>"5 minute walk",
"3"=>"10 minute walk",
"4"=>"No Bus Needed"
);
$option_train_stat = array( "1"=>"5 minute walk",
"2"=>"10 minute walk",
"3"=>"5 minute drive",
"4"=>"10 minute drive",
"5"=>"No Train Needed"
);
function select_box($k,$v){ //$k is the id and $v is the description for the select boxes label
$string = "option_"; //these two lines
$option_array =$string . $k; //are the troublemakers!
$buffer = '<select name="' . $k . '" id="' . $k . '">';
foreach ($option_array as $num=>$desc){
$buffer .= '<option value="' . $num . '">' . $desc . '</option>';
}//end foreach
$buffer .= '</select>';
$buffer .= '<label for="' . $k . '">' . $v . '</label>';
return $buffer;
}//end function
And the code which calls this function is:
function create_table($titles, $id) { //$titles is the relevant array from lists.php, $id is the id of the containing div
$select = array('timescale','bus_route','train_stat'); //'select' id list
$textarea = array('notes'); // 'textarea' id list
$buffer = '<div id="' . $id . '">';
foreach ($titles as $k=>$v) { //$k is the database/id/name $v is the description text
if (in_array($k,$select)){
$buffer .= select_box($k,$v);
}
else if (in_array($k,$textarea)){
$buffer .= text_box($k,$v);
}
else{
$buffer .= check_box($k,$v);
}
}
$buffer .= '</div>';
echo $buffer;
}
Add $ to eval the string.
$i_d = "first";
$string = "_array";
$myvar = $i_d . $string;
$$myvar = array('one','two','three');
print_r( $$myvar);
I'm trying to display the results of my query in a neatly formatted table but I'm pretty new to PHP so I'm at a bit of a loss. Currently I have this:
$dbconn = pg_connect("host=localhost port=5432 dbname=mary");
$result = pg_query($dbconn, $selectStmt);
$resultArr = pg_fetch_all($result);
print_r($resultArr);
Assume $selectStmt = SELECT State, Name FROM perez.pop WHERE Name LIKE '%Alabama%';
When I print this, I get the following:
Array ( [0] => Array ( [state] => 1 [name] => Alabama ) )
How could I place this into a table where the columns are "state" and "name" along with one more column in which I plan to place a link to a different page?
Also, can anyone clarify how the $resultArr looks when I get multiple rows as a result of my query?
EDIT: I'd like the output to look something like this:
State | Name | Follow link
___________________________________________________
32 Alabama <some link to a php page>
2 Alabama <another link>
You can just loop in your array and print your table while looping, something like
$resultArr = pg_fetch_all($result);
//print_r($resultArr);
echo '<table>
<tr>
<td>State</td>
<td>Name</td>
</tr>';
foreach($resultArr as $array)
{
echo '<tr>
<td>'. $array['State'].'</td>
<td>'. $array['Name'].'</td>
</tr>';
}
echo '</table>';
$query = pg_query("SELECT * FROM user");
$users_arr = pg_fetch_all($query);
// render thead
$thead = '<thead>
<tr>';
foreach($users_arr[0] as $key => $value) {
$thead .= '<th>' . $key . '</th>';
}
$thead .= '</tr>
</thead>';
// render tbody
$tbody = '<tbody>';
foreach($users_arr as $key => $value) {
$tbody .= '<tr>';
foreach($value as $k => $v) {
$tbody .= '<td>' . $v . '</td>';
}
$tbody .= '</tr>';
}
$tbody .= '</tbody>';
// render table
$table = '<table>' . $thead . $tbody . '</table>';
echo $table;