Rectangle 27 202

In the case of 2 nested loops:

break 2;
continue 2

How can I break an outer loop with PHP? - Stack Overflow

php for-loop break
Rectangle 27 35

just what i needed.. that "continue" for breaking only the current loop and not leaving the whole loop as break does.

Can you 'exit' a loop in PHP? - Stack Overflow

php loops
Rectangle 27 11

You can use break 2; to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.

How can I break an outer loop with PHP? - Stack Overflow

php for-loop break
Rectangle 27 5

$i = new MovieClip();
    foreach($movieClipArray as $i)
    {
          $nameArray = array();
          foreach($nameArray as $n) if($i->name==$n) break 2;
    }

Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).

refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "

How can I break an outer loop with PHP? - Stack Overflow

php for-loop break
Rectangle 27 2

$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo

// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2

for ($i = 0; $i < 16; $i += 1) {
    $cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
    $month_name = date('F Y', strtotime($cur_month . " months"));
    var_dump(month_name);
}

date - php - for loop for each month of year - Stack Overflow

php date loops for-loop
Rectangle 27 2

This should do the trick, its your code refactored into 2 functions, one for creating single rows and one for looping through your arrays to create multiple.

<?php
    function createTableRow($layout_name, $company_name){
        $tablerow = "
            <tr>
                <td>".$layout_name."</td>
                <td>".$company_name."</td>
            </tr>   
        ";
        return $tablerow
    }

    function printTableRows($banners){
        foreach($banners as $bannerID => $layoutInfo){
            if(isset($bannerArray[$bannerID])){
              foreach($bannerArray[$bannerID] as $id => $record){ 
                 echo createTableRow($record['layout_name'], $record['company_name']);
              }
            } else { 
                echo createTableRow($record['layout_name'], "");
            }
        }
    }
    if($hideempty == 'hide'){
        foreach($bannerArray as $bannerID => $records){
             foreach($records as $id => $record){
                  echo createTableRow($record['layout_name'], $record['company_name']);
             }
         }
     } else {
          $layoutGroupArray = mysqli_fetch_assoc(mysqli_query($link, "SELECT `set_layout_group` FROM `SetTopics` WHERE `set_id` = $setID"));
          $layoutGroup = $layoutGroupArray['set_layout_group'];
          printTableRows($banners[0]);
          printTableRows($banners[$layoutGroup]);
     }

 ?>

arrays - Use same code for multiple PHP foreach loops efficiently - St...

php arrays loops foreach
Rectangle 27 1

$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo

// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2

for ($i = 0; $i < 16; $i += 1) {
    $cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
    $month_name = date('F Y', strtotime($cur_month . " months"));
    var_dump(month_name);
}

date - php - for loop for each month of year - Stack Overflow

php date loops for-loop
Rectangle 27 2

Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.

$color = 0;
while($row = mysql_fetch_array($result)) {

  if($color % 2 == 1){
    echo "<tr>";
  }else{
    echo "<tr class=\"altRow\">";
  }
  echo "
  <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";
  $color++;
}

Defining a class on the table row is not a very efficient way of doing it. Most browsers have very different renderings for CSS properties on the table row. Otherwise this is a great way of doing it. P.S. $color++ looks a lot better than $color = $color + 1!

Agreed, I just couldn't remember if PHP had a ++ operand (been in .NET land for awhile). As far as style, I agree as well, IE is a pain with row styling. Just wanted to hammer out an example of how to use the mod operation.

PHP alternating colors - Stack Overflow

php
Rectangle 27 2

Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.

$color = 0;
while($row = mysql_fetch_array($result)) {

  if($color % 2 == 1){
    echo "<tr>";
  }else{
    echo "<tr class=\"altRow\">";
  }
  echo "
  <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";
  $color++;
}

Defining a class on the table row is not a very efficient way of doing it. Most browsers have very different renderings for CSS properties on the table row. Otherwise this is a great way of doing it. P.S. $color++ looks a lot better than $color = $color + 1!

Agreed, I just couldn't remember if PHP had a ++ operand (been in .NET land for awhile). As far as style, I agree as well, IE is a pain with row styling. Just wanted to hammer out an example of how to use the mod operation.

PHP alternating colors - Stack Overflow

php
Rectangle 27 2

Looping through the array won't be that efficient. If your database becomes too large then it slow down the entire process and also there might be a rare situation when 2 threads are looping through the array for the same random number and it will be found available and return same number to both the tickets.

So instead of looping through the array you can set the 10 digit registration id as primary key and instead of looping through the array you can insert the registration details along with randomly generated number, if the database insert operation is successful you can return the registration id but if not then regenerate the random number and insert.

Alternate solution which will be more effective Instead of 10 digit random numbers you can use timestamp to generate a 10 digit unique registration number and to make it random you can randomize the first 2 or 3 digits of the timestamp

php - How to generate unique random value for each user in laravel and...

php mysql laravel random eloquent
Rectangle 27 1

$entries = $topics->_embedded->entries; // 'entries' from the json response is an array. 
$i = 0;
while(isset($entries[$i])) { // Loop through the array to pick up all the data you need 
 $data[$i][0] = $entries[$i]->name;
 $data[$i][1] = $entries[$i]->description;
 $data[$i][2] = $entries[$i]->_links->self->href;
 $i++;
}
var_dump($data) // Array with all the data. Note that this is now a 2-d array.

Thanks @om_deshpande -- This works sort of, still get an error, but it outputs the names properly after that. Looks like this. Notice: Undefined offset: 5 in /Library/WebServer/Documents/afty/help/desk_api.php on line 125 array(5) { [0]=> string(18) "Privacy & Security" [1]=> string(16) "Canned Responses" [2]=> string(3) "FAQ" [3]=> string(22) "Suggestions & Feedback" [4]=> string(7) "General" }

@JeffSolomon ahh ok. Made a small edit. Added isset() inside the while statement. Should work now.

sorry @om_deshpande, one other question. In using your format I'm able to get the single element of "NAME" but if I want other elements from the JSON response within the ENTRIES for example like "description" for example which is the next element, I don't see how to do that with your formatting. Thanks for continued help.

@JeffSolomon if you want all other elements within ENTRIES, you can run a foreach loop inside the while loop. If you want just one or two others, you can specify each in a separate line inside the while loop - $name[$i][0] = $entries[$i]->name; $name[$i][1] = $entries[$i]->description; Let me know which one of the above are you looking for; I'll update the code in my answer accordingly.

php - Looping through JSON response from desk.com API - Stack Overflow

php json api
Rectangle 27 2

<?php
require('mc_table.php');

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
//Table with 27 rows and 2 columns since you have 27 values 
$pdf->SetWidths(array(30,60));

$pdf->Row(array('Key','Value'));
$pdf->Row(array('JobTitle','test-job'));
$pdf->Row(array()); //Do the same thing as above
.
.
.
.
//Do it for 27 times since you have 27 values
//Or store them in array and use a loop
$pdf->Output();
?>

Thanks, do you maybe now how I can change the padding in the cells of the row?

Maybe in the function 'Row' , you need to set the position of x and y accordingly.I'll check it

php - FPDF + associative array key value column - Stack Overflow

php arrays pdf fpdf
Rectangle 27 1

$count = 7; // number to show
// start at half threshold down from the current location.
$number = $current - round($count/2); 
if( $number > 1 ) echo '...';
else $ // increase to have number start at 1.
for( $number; $number < $number + $count; $number++)
{
    // your for loop as normal
}
if( $number < $total ) echo '...';

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Add Ellipsis in PHP Pagination - Stack Overflow

php pagination
Rectangle 27 1

Jetpack runs a partial template for the loop (e.g. content.php), retrieves the HTML output by AJAX, then appends it "live" to the current page. It never uses the original template (index.php) so it doesn't matter that you've written 2 distinct loops there.

You might be able write your own solution by borrowing Jetpack's scroll detection and AJAX requests and adapting the rest.

php - Modify WordPress Jetpack Plugin To Use 'Class' Instead Of An 'ID...

php javascript wordpress infinite-scroll jetpack