Hi I have a 1D array (1 by 20) that I would like to transform to a 2D Array (4 by 5)
$winning_number = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
to
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
right now I am using this code:
foreach ($wining_no as $boulex)
{
for($i=0;$i<$5;$i++)
{
if($i==0)
{
for($j=0;$j<$4;$j++)
{
$boule_array[$j][$i] = $boulex;
}
}
}
}
For some reason this does not work
You could use the array_chunk($array, $size) function
For you it would be like this
array_chunk($winning_number, 5);
Related
I require a nested for loop for my php website which gives me results as follows.
I want to two integers incrementally - one stops at 12 and other continues until the specified values.
If one string is $i and second string is $j than I want output as:
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 1 2 3 4 5 6 7 8 9 10 11 12
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 13 14 15 16 17 18 19 20 21 22 23 24
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 25 26 27 28 29 30 31 32 33 34 35 36
It should be repeated up to n values.
You can try this:
<?php
createMatrix(40);
function createMatrix($jMax)
{
$jVal = 0;
while ($jVal < $jMax) {
// print line $i
echo ('$i' . "\t");
for ($iVal = 1; $iVal <= 12; $iVal++) {
echo "$iVal\t";
}
echo "\n";
// print line $j
echo ('$j' . "\t");
for ($j = 1; $j <= 12; $j++) {
$jVal++;
echo "$jVal\t";
if ($jVal === $jMax) {
echo "\n";
break;
}
}
echo "\n";
}
}
OUTPUT:
I am using simple pagination with following code.
But I want to show 1 2 3 ..... 10 11 12 type pagination.
When more number of pages are there, pagination should be adjusted accordingly.
My current simple pagination php code is :
<?php
if (isset($_GET["start"])){
$start = $db->filter($_GET["start"]);
}
if(empty($start)){
$start= 0;
}
$maxrecords = 10;
$pagination_query = "select * from table order by id desc";
$pagination_count = $db->num_rows($pagination_query);
$query = "select * from table order by id desc limit $start,$maxrecords"; // For record to display...
$result = $db->get_results($query);
?>
<div class="pagination">
<?php
for($i=0;$i<ceil($pagination_count/$maxrecords);$i++){
if($start==$i*$maxrecords){
?>
<a class='active'> <?php print $i+1;?></a>
<?php }else{ ?>
<?php print $i+1;?>
<?php
}
}
?>
</div>
<!-- Rest code to display records goes here -->
Now this code is working rightly...
Only thing is it is showing paginatin - 1 2 3 4 5 6 7 like unlimited depending upon total records in database...
I want to convert it to 1 2 3 .... 22 23 24 etc...
Your valuable help appreciated...
My answer begins by some notes about what I'm seeing about pagers.
Then, this answer doesn't provide the "expected result" you want, but I think it could be more "flexible".
I think your query to get the number of record seems to be expensive. You could just use a count(*) :
$pagination_query = "select count(*) as num from table order by id desc" ;
$pagination_count = $db->get_field_result() ; // I don't know your DB API.
I think your query to get the current results seems to doesn't take care about the $start and $maxrecords (but I don't known your database API) :
$query = "select * from table order by id desc limit ".($start*$maxrecords).",$maxrecords"; // range 0+10, 10+10, 20+10, ...
You could use several loops to display begining pages, "around current", and ending pages.
Here is a "sample" code, not perfect, but it could helps you to imaging your own pager.
$start = 7 ; // try with 1, 7, 24, 75...
$pagination_count = 40 ; // Number of results
$maxrecords = 10 ;
$num_pages = ceil($pagination_count / $maxrecords) ;
// echo "num_pages=$num_pages\n" ; // Just for dev
if ($start > $num_pages) { $start = $num_pages; }
if ($num_pages < 6) {
for ($i = 0; $i < $num_pages ; $i++) {
echo render_page_link($i, $start);
}
}
else {
// Begining
for ($i = 0 ; $i < min(3, $num_pages); $i++) {
echo render_page_link($i,$start) ;
}
echo ' (...) ' ;
$have_middle = ($start > 3 && $start <= $num_pages - 3) ;
if ($have_middle) {
// Around current
for ($i = max(3, $start - 3); $i < min($start + 3 - 1, $num_pages - 3) ; $i++) {
echo render_page_link($i,$start) ;
}
}
// Ending
if ($have_middle) echo ' (...) ' ;
for ($i = $num_pages - 3; $i < $num_pages ; $i++) {
echo render_page_link($i,$start);
}
}
echo "\n"; // just because I test on CLI.
// Here is a little function to display the link:
// Currently just "plain text", but could be <a> or <span> with CSS...
function render_page_link($index, $current = -1)
{
if ($index != $current - 1) return ($index+1) . " " ;
return "[".($index+1)."] " ;
}
Example result for 1 to 24 pages
[1] 2 3 (...) 22 23 24
1 [2] 3 (...) 22 23 24
1 2 [3] (...) 22 23 24
1 2 3 (...) [4] 5 6 (...) 22 23 24
1 2 3 (...) 4 [5] 6 7 (...) 22 23 24
1 2 3 (...) 4 5 [6] 7 8 (...) 22 23 24
1 2 3 (...) 5 6 [7] 8 9 (...) 22 23 24
1 2 3 (...) 6 7 [8] 9 10 (...) 22 23 24
1 2 3 (...) 7 8 [9] 10 11 (...) 22 23 24
1 2 3 (...) 8 9 [10] 11 12 (...) 22 23 24
1 2 3 (...) 9 10 [11] 12 13 (...) 22 23 24
1 2 3 (...) 10 11 [12] 13 14 (...) 22 23 24
1 2 3 (...) 11 12 [13] 14 15 (...) 22 23 24
1 2 3 (...) 12 13 [14] 15 16 (...) 22 23 24
1 2 3 (...) 13 14 [15] 16 17 (...) 22 23 24
1 2 3 (...) 14 15 [16] 17 18 (...) 22 23 24
1 2 3 (...) 15 16 [17] 18 19 (...) 22 23 24
1 2 3 (...) 16 17 [18] 19 20 (...) 22 23 24
1 2 3 (...) 17 18 [19] 20 21 (...) 22 23 24
1 2 3 (...) 18 19 [20] 21 (...) 22 23 24
1 2 3 (...) 19 20 [21] (...) 22 23 24
1 2 3 (...) [22] 23 24
1 2 3 (...) 22 [23] 24
1 2 3 (...) 22 23 [24]
I have a problem with aligning numbers from a multidimensional array. I want to print the following result:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
And I want all of the numbers to be aligned with the second digit of the next rows. However my result is that:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
I did this in C# by using:
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write("{0,4}", matrix[row, col]);
}
But how can I receive this result in PHP?
You can use str_pad
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
$test = '';
foreach ($arr as $key => $value) {
if ($key % 4 == 0) {
$test .= "\n";
}
$test .= str_pad($value, 4, ' ', STR_PAD_LEFT);
}
echo "<pre>$test</pre>";
The result would be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Im trying to make a table consist some sort of cinema seat from A[1-20] to J[1-20]. I have a txt file which contain seats that are reserved, like this:
A2;
A1;
A3;
A7;
if a seat is reserved, the bgcolor of table must be red. This is the full code:
<?php
$file = fopen($path,"r") or exit("cant open file");
$seat="";
while(!feof($file))
{
$seat.= fgets($file);
}
$seat_splitted = explode(";",$seat);
fclose($file);
$arrTable[]="";
$letter="";
$tableContent="";
for($i = 0,$counter=0;$i<10;$i++,$counter++)
{
if($i==0)
$letter="A";
else if($i==1)
$letter="B";
else if($i==2)
$letter="C";
else if($i==3)
$letter="D";
else if($i==4)
$letter="E";
else if($i==5)
$letter="F";
else if($i==6)
$letter="G";
else if($i==7)
$letter="H";
else if($i==8)
$letter="I";
else if($i==9)
$letter="J";
?>
<tr>
<?php
for($j = 1;$j<21;$j++)
{
$arrTable[$counter]= $letter.$j;
foreach($seat_splitted as $value)
{
if(strcmp($value,$arrTable[$counter])==0)
//if($value == $letter.$j)
{
$GLOBALS['color']="red";
break;
}
else
$GLOBALS['color']="white";
}
?>
<td bgcolor="<?php echo $GLOBALS['color']; ?>"> <?php echo $arrTable[$counter]?> </td>
<?php
$counter++;
}
?>
</tr>
<?php
}
?>
I dont know why when if(strcmp($value,$arrTable[$counter])==0) or //if($value == $letter.$j), its only catch the first seat which is "A2". But when i change the txt file to this:
A2;A1;A3;A7;
the IF can catch them all. Is it wrong to concatenate string like this? $seat.= fgets($file);. What can i do to make it work with the first txt file? Sorry for my bad english.
I think something like this should work for you. I simplified everything a bit.
1. First I create an array ($seats) with all seats in it. Which has a structure like this:
Array
(
[A] => Array
(
[1] => free
[2] => free
[3] => free
//...
//...
2. After this I get all reserved seats from the file into an array. (A better solution would be if you have all this data in a database!)
3. Then I loop through each reserved seat and also set it to that in the $seats array, so that it gets something like this:
Array
(
[A] => Array
(
[1] => reserved
[2] => free
[3] => reserved
//...
//...
4. At the end I simply print the table
Code:
<?php
//create seats
$rows = range(strtoupper("A"), strtoupper("J"));
$columns = range(1, 20);
$seats = array_combine($rows, array_map(function($v)use($columns){
return array_combine($columns, array_fill(0, count($columns), "free"));
}, $rows));
//get reserved seats
$reservedSeats = array_map(function($v){
return trim($v, ";");
}, file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
//set reserved seats
foreach($reservedSeats as $reserved) {
$checkOne = preg_replace("/[^A-Z]*/", "", $reserved);
$checkTwo = preg_replace("/[^0-9]*/", "", $reserved);
if(isset($seats[$checkOne]) && isset($seats[$checkOne][$checkTwo]))
$seats[$checkOne][$checkTwo] = "reserved";
}
//print seats
$reservedColor = "red";
$defaultColor = "white";
$rowColor = "green";
echo "<table border='1'>";
foreach($seats as $row) {
echo "<tr><td bgcolor='" . $rowColor . "'>" . $rowKey . "</td>";
foreach($row as $key => $seat)
echo "<td bgcolor='" . ($seat == "reserved"?$reservedColor:$defaultColor) . "'>" . $key . "</td>";
echo "</tr>";
}
echo "</table>";
?>
output (without colors):
A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
D 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
E 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
F 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
G 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
H 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
IF you use the explode as you do, you will get an array of:
["A2","\nB1","\nC3"...]
So you have to deal with the \n
Maybe you could have a try as explode(";\n",$seat)
Update: in some system u need to use \r\n. So here is the correct final solution: explode(";\r\n",$seat)
How do i create a nested loop that will output the following numbers?
each round the inner loop increase from 1 to 5, 5 to 10 and so on.
from i = 0 to 5
inner loop:
result: 1 2 3 4 5
result: 6 7 8 9 1 0
result: 11 12 13 14 15
result: 16 17 18 19 20
next
for($i=0;$i<50;$i++)
{
$s = $i +5;
echo $s;
}
unless you have to use an inner do...while loop, this will work:
<?php
$max = 5;
for($i=0;$i<$max;$i++){
for($j=1;$j<=$max;$j++){
echo str_pad(($i*$max)+$j,4);
}
echo "\r\n";
}
output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
I used str_pad() just to make the columns more uniform