I have a HTML table with more than 1000 rows. Now i want to show these records in parallel manner.
Like 30 rows in left side and 30 in right side
1 xyz 120 00:10:01 31 xyz 120 00:10:01
1 xyz 120 00:10:01 32 mxy 20 00:10:01
2 mxy 20 00:10:01 . . . ........
. . . ........ . . . ........
. . . ........ . . . .........
. . . ........ . . . .........
30 mld 2 00:05:01 60 mld 2 00:05:01
I am going to generate PDF so i want to show 60 records per page. 30 left and 30 right.
It will probably be easiest to display two tables side by side (set each to a width of about half the page and float one to the left or right).
Then your loop can be simple:
$i = -1;
$totalRows = count($rows);
$halfRows = round($numRows / 2);
//construct $headerRow HTML here
foreach ($rows as $row) {
$i++;
if ($i == 0 || $i == $halfRows) {
echo '<table class="'. ($i==0 ? 'floatLeft': '').'">';
echo $headerRow;
}
//Code to output column values here
if ($i == ($halfRows - 1)) {
echo '</table>';
}
}
echo '</table>';
Related
I want to create an array with the help of MYSQL and display it the following way:
while ($array = mysqli_fetch_assoc($res))
{
echo $array["first_name"] . ", "
. $array["last_name"] . ", "
. $array["personal_id"] . ", "
. $array["salary"] . ", "
. $array["birthday"] . "<br />";
}
To improve usability, I only want to display 10 results, put a link to the next 10 results at the bottom and display these on another page.
How do I have to change the while-condition in order to only display the first 10 results of my result?
I tried searching for a solution but I couldn't find anything that fits my needs, so any help is appreciated!
Thanks in advance!
Check offset and limit conditions in mysql.
Example: to show 16 - 26 records from result query.
$sql = "SELECT * FROM Table LIMIT 10 OFFSET 15"
If what you want is just a way to break out of the loop after the first 10 results.
$counter = 1;
while (($array = mysqli_fetch_assoc($res)) || ($counter < 10))
{
echo $array["first_name"] . ", "
. $array["last_name"] . ", "
. $array["personal_id"] . ", "
. $array["salary"] . ", "
. $array["birthday"] . "<br />";
$counter++;
}
I have an array of menu items that I'm printing out to the screen using a foreach loop into a single, continuous, unordered list. Because of CSS/design, they display on the screen in 3 columns in this order...
1 2 3
4 5 6
7 8 9
But I need them to display like this...
1 4 7
2 5 8
3 6 9
Is there a way I can, without changing the HTML structure and still within a single for/foreach loop, print out the same items so that they are arranged in the latter configuration?
Example code
<ul>
<?php
foreach($nav_menu as $nav):
echo "<li><a href='" . $nav->url . "'>" . $nav->title . "</a></li>";
endforeach;
?>
</ul>
You can try this:
<ul>
<?php
foreach($nav_menu as $index => $nav):
$array_index = ($index / 3) + 3 * ($index % 3);
echo "<li><a href='" . $nav_menu[$array_index]->url . "'>" . $nav_menu[$array_index]->title . "</a></li>";
endforeach;
?>
</ul>
Gives you want you say you want... :)
$n = 1;$t=0;
for($l=1;$l<=9;++$l){
echo $n; ++$t;
if($t==3){
$n=($n-5);
echo '<br>';
$t=0;
}
else{
$n = $n + 3;
}
}
I'm posting this more generalized solution for future reference, and it's based on x01saa's answer, which is still the correct one given the question I asked.
<?php
$cols = 3; // this could be any positive integer
$rows = ceil((float)count($nav_menu)/(float)$cols);
foreach($nav_menu as $index => $nav):
$array_index = ($index / $cols) + $rows * ($index % $cols);
echo "<li><a href='" . $nav_menu[$array_index]->url . "'>" . $nav_menu[$array_index]->title . "</a></li>";
endforeach;
?>
Consider I have a following array of total scores, with each value being a score of a player in a tournament.
$total_scores = array(350,200,150,150,75,75,75,0);
I need to create a table, which lists the players with correct positions, if they have the same score, the listing should reflect this, ie.:
1. Player 1 350
2. Player 2 200
3.-4. Player 3 150
3.-4. Player 4 150
5.-7. Player 5 75
5.-7. Player 6 75
5.-7. Player 7 75
8. Player 8 0
I tried to do something with
foreach ($total_scores as $total_score) {
$no_of_occurrences = array_count_values($total_scores)[$total_score];
}
But cannot figure out how to build the correct positions numbering.
<?php
$scores = array(350,200,150,150,75,75,75,0); //assuming you have sorted data otherwise you need to sort it first
$count = array();
$startIndex = array();
$endIndex = array();
$len = count($scores);
for($i = 0; $i < $len; $i++){
if(!isset($count[$scores[$i]])){
$count[$scores[$i]] = 1;
$startIndex[$scores[$i]] = $endIndex[$scores[$i]] = $i+1;
}else{
$count[$scores[$i]]++;
$endIndex[$scores[$i]] = $i+1;
}
}
$i = 1;
foreach($scores as $s){
echo $startIndex[$s].'.';
if($startIndex[$s] != $endIndex[$s]){
echo '-'.$endIndex[$s].'.';
}
echo ' Player '.$i.' '.$s."\n"; //if newline not works try echoing <br>
$i++;
}
Working Demo
For this Array needs to be sorted in descending order
$total_scores = array(350, 200, 150, 150, 75, 75, 75, 0);
rsort($total_scores);
$no_of_occurrence = array_count_values($total_scores);
array_unshift($total_scores, ""); // For starting count from 1
unset($total_scores[0]); // For starting count from 1
$i = 1;
foreach ($total_scores as $key => $value)
{
$position = array_keys($total_scores,$value);
if($no_of_occurrence[$value] == 1)
{
echo "Position " . $i . " ";
echo "Player " . $i . " " . $value . " ";
}
else
{
echo "Position " . $position[0] . " - " . end($position) . " ";
echo "Player " . $i . " " . $value . " ";
}
$i++;
echo "<br>";
}
Output of above code :
Position 1 Player 1 350
Position 2 Player 2 200
Position 3 - 4 Player 3 150
Position 3 - 4 Player 4 150
Position 5 - 7 Player 5 75
Position 5 - 7 Player 6 75
Position 5 - 7 Player 7 75
Position 8 Player 8 0
I recently asked a question about how to parse this JSON feed. An answer was given but it also presented another problem. The echo is spitting out duplicate records for each player in the feed. I'm not sure why this is happening and I hope someone can help me out.
Here's my code:
$url = file_get_contents("http://www.nfl.com/liveupdate/game-center/2015091700/2015091700_gtd.json");
$json = json_decode($url, true); // 'true' makes data an array
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
$player = array();
foreach($iterator as $key=>$value) {
$player[$key] = $value;
echo $player['name'] . ' ' . $player['att'] . ' ' . $player['cmp'] . ' ' . $player['yds'] . ' ' . $player['tds'] . ' ' . $player['fgm'] . ' ' . $player['fga'] . '<br>';
}
This is the JSON:
{
"2015091700":{
"home":{
"abbr":"KC",
"to":0,
"stats":{
"passing":{
"00-0023436":{
"name":"A.Smith",
"att":25,
"cmp":16,
"yds":191,
"tds":0,
"ints":2
}
},
"rushing":{
"00-0026213":{
"name":"J.Charles",
"att":21,
"yds":125,
"tds":1
}
}
}
}
}
}
It is giving me duplicates. See below.
A.Smith
A.Smith 25
A.Smith 25 16
A.Smith 25 16 191
A.Smith 25 16 191 0
A.Smith 25 16 191 0
A.Smith 25 16 191 0
A.Smith 25 16 191 0
J.Charles 25 16 191 0
J.Charles 21 16 191 0
J.Charles 21 16 125 0
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
I would like unique results for each player.
A.Smith should be A.Smith 25 16 191 0 2 and J.Charles should be J.Charles 21 125 1 instead of what you see above.
The code is not actually creating duplicates, you just print out every intermittent result. In fact, the loop is overwriting data for existing keys in every iteration. This would work if you only have data for one player, but can lead to unexpected (wrong) results in this case with multiple players.
A quick and somewhat dirty solution would be to save and reset when a new player is started. Here we assume the 'name' key is always present and always the first entry.
$url = file_get_contents("http://www.nfl.com/liveupdate/game-center/2015091700/2015091700_gtd.json");
$json = json_decode($url, true); // 'true' makes data an array
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
$players = array();
$player = false;
foreach ( $iterator as $key => $value ) {
// The key 'name' marks the start of a new player
if ( $key == 'name' ) {
// If we were already processing a player, save it
if ( is_array($player) ) {
$players[] = $player;
}
// Start a new player
$player = array();
}
// If we are processing a player, save the values
if ( is_array($player) ) {
$player[$key] = $value;
}
}
// Don't forget the last player
$players[] = $player;
// Output the resulting players
print_r($players);
// Or, in your desired output format
// Will give a ton of E_NOTICES on dev setups!
foreach( $players as $player ) {
echo $player['name'] . ' ' . $player['att'] . ' ' . $player['cmp'] . ' ' . $player['yds'] . ' ' . $player['tds'] . ' ' . $player['fgm'] . ' ' . $player['fga'] . '<br>';
}
It would be cleaner to actually get the data from the parsed array directly, but this requires a well defined and known format for the json.
Hi i have the table structure as follows
id --auto increment id
alphabet
user_id
name
and while adding i needs to insert the english alphabets to users as incremental order like this A,B,C,....Z,AA,AB,AC,...AZ,BA,BB,BC,.....BZ.
I need to get like this
id alphabet user_id name
------------------------------------------
1 A 1 name-1
2 B 1 name-2
. . . .
. . . .
26 Z 1 name-n
27 A 2 name-1
28 B 2 name-2
. . . .
. . . .
52 Z 2 name-52
53 AA 2 name-53
. . . .
. . . .
. . . .
. AZ 2 .
Thanks for your time.
It's quite easy that you use php to implement this,
please check codes below
$i = 'A';
echo $i;
echo PHP_EOL;
while($i != 'ZZ')
{
echo ++$i;
echo PHP_EOL;
}
or see the results in http://codepad.org/QB4kyV7U
Check this similar article:
Algorithm to get the excel-like column name of a number
Here's a nice simple recursive function (Based on zero indexed numbers, meaning 0 == A, 1 == B, etc)...
function getNameFromNumber($num) {
$numeric = $num % 26;
$letter = chr(65 + $numeric);
$num2 = intval($num / 26);
if ($num2 > 0) {
return getNameFromNumber($num2 - 1) . $letter;
} else {
return $letter;
}
}
And if you want it one indexed (1 == A, etc):
function getNameFromNumber($num) {
$numeric = ($num - 1) % 26;
$letter = chr(65 + $numeric);
$num2 = intval(($num - 1) / 26);
if ($num2 > 0) {
return getNameFromNumber($num2) . $letter;
} else {
return $letter;
}
}
Tested with numbers from 0 to 10000...
Sounds a lot like this SO article
MYSQL: how to "reorder" a table
Is there an absolute necessity to do what you are wanting when you can use sorting to output your data however you want?