I want to display 3 uls:
$one = '<ul>';
$j = 0;
while($j<3){
$one .= '<li data-src="' . $myarray[0][$j] . '"></li>';
$j++;
}
echo $one;
echo '</ul>';
$two = '<ul>';
$j = 0;
while($j<3){
$two .= '<li data-src="' . $myarray[1][$j] . '"></li>';
$j++;
}
echo $two;
echo '</ul>';
$whatever = '<ul>';
$j = 0;
while($j<3){
$whatever .= '<li data-src="' . $myarray[2][$j] . '"></li>';
$j++;
}
echo $whatever;
echo '</ul>';
How can I combine the above in shorter way?
Think about what you want to display first. Here you will have 3 lists, each of your 3 list containing a single sub-list. So you need to loop through your $myarray (which is a 2D array) :
<?php $j = 0; ?>
<?php foreach( $myarray as $row ): ?>
<ul>
<li data-src="<?php echo $row[$j] ?>"></li>
</ul>
<?php endforeach ?>
foreach will go through your first dimension, so you only deal with the next dimension, which make you $row becoming a one-dimension array instead of 2 which is lighter to deal with.
This solution is templating-oriented, which suit well when you need to separate your logic from your graphic.
Like so:
for ($i = 0; $i < 3; $i++) {
echo '<ul>';
for ($j = 0; $j < 3; $j++) {
echo '<li data-src="' . $myarray[$i][$j] . '"></li>';
}
echo '</ul>';
}
$html = "";
for($i=0;$i<=2;$i++) {
$html .= '<ul>';
$j = 0;
while($j<3){
$html .= '<li data-src="' . $myarray[$i][$j] . '"></li>';
$j++;
}
$html .= '</ul>';
}
echo $html;
Something like this springs to mind:
$i = 0;
$j = 0;
while($i < 3) {
echo '<ul>';
while ($j < 3) {
echo '<li data-src="' . $myarray[$i][$j] . '"></li>';
$j++;
}
echo '</ul>';
$i++;
$j = 0;
}
Since you have two loops (which you could also replace with for-loops), you can nest them to only type your html once.
This way you also don't need to store the html in strings. You can echo them right away.
If I understand correctly, you have the same code repeating three times. You can simply replace it with a nested for / while loop:
$i= 0;
while ($i < 3) {
echo '<ul>';
$j = 0;
while ($j < 3) {
echo '<li data-src="' . $myarray[$i][$j] . '"></li>';
$j++;
}
echo '</ul>';
$i++;
}
Related
I am trying to print an array so every 6th element is on a new line. I'm trying to do this in a table. I would prefer if it could be answered in PHP, but Javascripts Ok.
$array_control = array(
"1","5","6","2","1",
"2","1","6","4","3",
"3","2","5","6","6",
"4","3","1","5","4",
"6","4","2","3","6"
);
$arrayLength = count($array_control);
$i = 0;
while ($i < $arrayLength)
{
if ($i==0) {
echo "<tr>";
}
if (is_int($i/5)) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
echo "</tr>";
echo "<tr>";
}else{
echo '<td style="width:16%;">'.$array_control[$i-1].'</td>';
}
$i++;
}
Any help would be great!
The %-operator (modulus) solves this for you:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">';
if( ($i % 6) == 0){
echo $array_control[$i-1] . '</td></tr><tr>';
} else{
echo $array_control[$i-1] . '</td>';
}
}
echo '</tr></table>';
or if you feel sassy:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">' . $array_control[$i-1], ( ($i % 6) == 0 ) ? '</td></tr><tr>' : '</td>';
}
echo '</tr></table>';
A for loop seems better, and maybe the mod operator is more clear.
echo '<tr>';
for ($i < = 0; i < $arrayLength; ++ $i) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
if ($i % 6 == 5) {
echo '</tr>';
echo '<tr>';
}
}
echo '</tr>';
This isn't quite ideal, because you'll get an empty '' at the end, but the browser should hide that. A little more logic can be added if you need to eliminate that too.
Further, with the array structure you're showing, you could eliminate the $arrayLength variable entirely, and instead use:
foreach ($array_control as $i => $val) {
echo '<td style="width:16%;">'.$val.'</td>';
I know, the headline is not very specific, but I dont't know how to title it.
I want to create a list from 1 to 10. All those numbers should be clickable and open a sublist. Again with the numbers from 1-10. Like this:
1
2
1
2
3
and so on...
Here is some code, I've got:
<?php
$num = $_GET['zahl'];
$zahlen = array();
while($num != 0){
$part = $num % 10;
array_push($zahlen, $part);
$num = floor($num/10);
}
foreach ($zahlen as $key => $value) {
$runs = $value + 1;
for ($i=1; $i < $runs ; $i++) {
if ($i == 1) {
echo $ulon . "\n";
}
echo $lion . "\n";
echo $a . "ordner.php?zahl=" . $i . $amiddle . $i . $aoff . "\n";
echo $lioff . "\n";
}
for ($i=1; $i < 11 ; $i++) {
if ($i == 1) {
echo $ulon;
}
echo $lion . "\n";
echo $a . "ordner.php?zahl=" . $zahlen[0] . $i . $amiddle . $i . $aoff;
echo $lioff . "\n";
}
echo $uloff;
echo "\n" . "</body>";
?>
What you wish to achieve should be fairly straigtforward with Javascript rather than PHP - the following uses PHP only to generate the initial list and subsequent sub-lists are generated using javascript.
<?php
echo "<ul id='infinite-menu'>";
for( $i=1; $i < 11; $i++ ){
echo "<li>$i</li>";
}
echo "</ul>";
?>
<script>
var children=10;
function newnodes(e){
if( e.target.childNodes.length==1 ){
var ul=document.createElement('ul');
e.target.appendChild(ul);
for( i=1; i < children+1; i++ ){
var li=document.createElement('li');
li.innerHTML=i;
ul.appendChild( li );
}
}
}
var col=document.querySelectorAll('ul#infinite-menu li');
if( col )for( n in col )if( col[n].nodeType==1){
col[n].addEventListener('click',newnodes,false);
}
</script>
This is my solution based on php. Thanks for your comments!
<body>
<?php
$open = isset($_GET["open"]) ? $_GET["open"] : "";
$openArr = explode(",",$open);
function liste($openArr, $link) {
$firstValue = array_shift($openArr);
echo "<ul>";
for ($i = 0; $i < 10; $i++) {
echo "<li>";
echo "<a href='rekursion.php?open=".$link.$i."'>$i</a>";
if (isset($firstValue) && $firstValue == $i) {
liste($openArr, $link.$i.",");
}
echo "</li>";
}
echo "</ul>";
}
liste($openArr,"");
?>
</body>
I add some Data in form of a table to my website. Actually everything works but i wanted to test the performance for some big data. It was very bad. Here my Results:
tablesize | time
-----------------
10x20 | 0.22s
100x20 | 3.29s
1000x20 | 28.75s
My question is which way is better to show the results and why:
1:
<?php
for($i=0; $i<count($array); $i++){
echo "<div id='...' class='...Ä style='...'>"
for($i=0; $i<count($array); $i++){
echo "<div style='...'>".array[$i][$j]."</div>";
}
echo "</div>";
}
?>
2:
<?php
for($i=0; $i<count($array); $i++){
?>
<div id='...' class='...Ä style='...'>
<?php
for($i=0; $i<count($array); $i++){
?>
<div style='...'> <?php echo array[$i][$j]; ?> </div>
<?php } ?>
</div>
<?php } ?>
Maybe someone has some additional tips how i can make the data visualization fast. I read some artical about MySQL structure and i believe my DB looks fine. So what i can do to optimize the reading of data.
As scaisEdge said, paginate your data.
Moreover, there are several changes that can make your code faster.
count($foo) outside of for loop
use pre-increment instead of post
use 'str' and concatenation instead of "str"
Here is a "benchmark" (without output) :
<?php
$array = array();
for($i = 0; $i < 1000; ++$i){
$array[] = range(0, 20, 1);
}
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
for($i=0; $i<count($array); $i++){
$a = "<div id='...' class='...Ä style='...'>";
for($j=0; $j<count($array[$i]); $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'Count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; $i++){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and pre : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = '<div id="..." class="...Ä style="...">';
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = '<div style="...">'.$array[$i][$j].'</div>';
}
$a = '</div>';
}
}
echo 'No count and pre and \' : ' . (microtime(true) - $t) . "\n";
// #scaisEdge
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
foreach($array as $subarray){
$a = '<div id="..." class="...Ä style="...">';
foreach($subarray as $v){
$a = '<div style="...">'.$v.'</div>';
}
$a = '</div>';
}
}
echo 'Foreach : ' . (microtime(true) - $t) . "\n";
?>
This outputs :
Count and post : 46.050459861755
No count and post : 30.590306043625
No count and pre : 29.880299091339
No count and pre and ' : 29.930299043655
Foreach : 29.120290994644
As weird as it looks, ' seems to be slower than ".
PHP checks " strings variables inside and replace it whereas ' string are just... string. No operation performed.
Has someone an idea of this slowness ?
For large output you should use pagination ... output 1000 rows seems not useful
If you paginate your result is more equal to the firts that to the last .
But you can do somethings for the code .. you should count the result just one time and not for all cycle of the loop
so instead of for($i=0; i<count($array); $i++){
you should use
$numElem =count($array);
for($i=0; $i<$numElem; $i++){
.......
}
or use
foreach ( $array as $key => $value ){
......
}
these are much faster
I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:
Item 1 Item 5 Item 9
Item 2 Item 6 Item 10
Item 3 Item 7 Item 11..............
Item 4 Item 8
Update to Meldin answer
$cols = 3; // Number of columns
$len = ceil(count($items) / $cols);
echo '<ul class="floatleft">';
for ($i = 0; $i < count($items); $i++) {
if ($i % $len == 0)
echo '</ul><ul class="floatleft">';
echo "<li>" . $items[$i] . "</li>";
}
echo '</ul>';
Also you can try this using two foreach() loops
$len = ceil(count($items)/3);
$itm = array_chunk($items,$len,true);
foreach($itm as $cols){
echo '<div class="floatleft">';
echo '<ul>';
foreach($cols as $col){
echo '<li>'.$col.'</li>';
}
echo '</ul>';
echo '</div>';
}
Use this
echo '<ul class="floatleft">';
for($i=0;$i<count($items);$i++){
if($i%4==0)
echo '</ul><ul class="floatleft">';
echo "<li>".$items[$i]."</li>";
}
echo '</ul>';
Try this:
$total = 10;
$cols = 3;
$rows = ceil($total/$cols);
echo "<table>";
for($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for($j = 1; $j <= $cols; $j++) {
$value = $i + 4*($j-1);
if ($value <= $total) {
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
echo "</table>";
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
I have created a for loop.
I am trying to loop through lots of members and create a row for each member in a table.
It's looping through too many times.
Is it the right type of loop to use?
<?php
for ($i = 1; $i = count($u); $i++)
{
?>
<tr>
<?php
echo "<td>$i</td>";
?>
<?php
foreach ($u as $username)
{
echo "<td>$username</td>";
}
?>
<?php
foreach ($p as $points)
{
echo "<td>$points</td>";
}
?>
</tr>
<?
}
?>
$u and $p are arrays.
Thanks
You can shorten it a bit and remove the inner loops:
<php
for ($i = 1; $i <= count($u); $i++)
{
echo '<tr>';
echo "<td>$i</td>";
echo "<td>$u[$i]</td>";
echo "<td>$p[$i]</td>";
echo '</tr>';
}
?>
$i = count($u) in the loop is what's causing the problem:
for ($i = 1; $i = count($u); $i++)
On every iteration of the loop, you're assigning count($u) to $i using a single =. Should be
for ($i = 1; $i <= count($u); $i++)
or
for ($i = 0; $i < count($u); $i++)
<?php
$count = count($u);
for ($i = 1; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $i . '</td>';
foreach ($u as $username) {
echo '<td>' . $username . '</td>';
}
foreach ($p as $points) {
echo '<td>' . $points . '</td>';
}
echo '</tr>';
}
?>
You should count $u before you loop it.