PHP for inner loop - php

I have 2 arrays, the first array was working as expected (looping through the array updating the db with all elements inserted) But I added a second array that has a value I basically wanted this value to merge to the output of array 1 (I use this to add to the db, it's a string value). This is driven by a form so only rows that are selected should be added.
so array 1 is item, description... and array 2 is hashtag added
I would like for only rows that are selected (item1, description1, hashtag1.. item4, description4, hashtag4 ... item5, description5, hashtag5 if we selected rows 1,4 and 5... it could be rows 7,8,9,10 or 1,2,3... all depends)
My code outputs like (hashtag1, item1, description1 and hashtag1, item4, description4 and hashtag1, item5, description5... hashtag2, item1, description1 and hashtag2, item4, description4 and hashtag2, item5, description5 so on... repeating hashtag for each loop 3 times this case)
So instead of 3 times it is 9 times repeating the same hashtag 3 times
$checked = $_POST['check'];
$changed = $_POST['hashtag'];
$finalArray;
$changedVal;
$count = count($changed);
$count2 = count($checked);
for ($i = 0; $i < $count; $i++) {
if (!empty($changed[$i])
&& isset($changed[$i])
&& !is_null($changed[$i]))
{
echo "<br>" . "it happens" . "<br>";
for ($jj =0; $jj < $count2; $jj++) {
$resultVal = $changed[$i] . "&&&" . $checked[$jj];
$unbinditem = array_pad(explode("&&&",$resultVal), 5, null);

Related

exclude array numbers for mysql

i have in mysql
id = 1
id_bets = 3
id_user = 3
numbers_sell = 4,7,9,1
I need in my php select all number for bets and exclue numbers sell
ex: bets have 50 numbers i nedd show
2 3 5 8 10 11 12 etc.....
exlude - 4,7,9,1
my code
for ($i=1; $i<=50; $i++) {
$exclude = array(4,7,9,1);
if(in_array($i, $exclude)) continue;
echo $i;
}
no work, array not accept while my sql
$exclude = array($row['numbers_sell');
no work
Use explode() to split up the value from the table column.
$exclude = explode(',', $row['numbers_sell']);

the inner for loop does not work after the 1st iteratin of the outer for loop

I try to insert values from a database into a 2d array in php, but the inner for loop does not work after the 1st iteration of the outer loop. Here is the code
for ($i=0; $i<$recCount; $i++) {
$row=mysqli_fetch_array($result, MYSQLI_ASSOC);
$orderNo[$i] = $row['orderNo'];
$orderDate[$i] = $row['orderDate'];
$subTotal[$i] = $row['subTotal'];
$discount[$i] = $row['discount'];
$deliveryCharge[$i] = $row['deliveryCharge'];
$grandTotal[$i] = $row['grandTotal'];
$k = 0;
for ($j=0; $j<$recCount2; $j++){
$row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
$orderNo2[$i][$j] = $row2['orderNo'];
echo "orderNo2[$i][$j]: " .$orderNo2[$i][$j]. "&nbsp";
if ($orderNo2[$i][$j] == $orderNo[$i]){
/*$subNo($i,$k) = $row2['subNo'];
$ItemNo($i,$k) = $row2['ItemNo'];
$Description($i,$k) = $row2['Description'];
$Qty($i,$k) = $row2['Qty'];
$Price($i,$k) = $row2['Price'];
$Amount($i,$k) = $row2['Amount'];*/
$k++;
}
}
$recCount = 3, $orderNo[$i] has the value of 1, 2, 3. $recCount2 = 15, the $orderNo2[$i][$j] should have five 1s, five 2s, and five 3s for each $i, but the echoed result of orderNo2 is
orderNo2[0][0]: 1 orderNo2[0][1]: 1 orderNo2[0][2]: 1 orderNo2[0][3]: 1 orderNo2[0][4]: 1 orderNo2[0][5]: 2 orderNo2[0][6]: 2 orderNo2[0][7]: 2 orderNo2[0][8]: 2 orderNo2[0][9]: 2 orderNo2[0][10]: 3 orderNo2[0][11]: 3 orderNo2[0][12]: 3 orderNo2[0][13]: 3 orderNo2[0][14]: 3 orderNo2[1][0]: orderNo2[1][1]: orderNo2[1][2]: orderNo2[1][3]: orderNo2[1][4]: orderNo2[1][5]: orderNo2[1][6]: orderNo2[1][7]: orderNo2[1][8]: orderNo2[1][9]: orderNo2[1][10]: orderNo2[1][11]: orderNo2[1][12]: orderNo2[1][13]: orderNo2[1][14]: orderNo2[2][0]:enter code here orderNo2[2][1]: orderNo2[2][2]: orderNo2[2][3]: orderNo2[2][4]: orderNo2[2][5]: orderNo2[2][6]: orderNo2[2][7]: orderNo2[2][8]: orderNo2[2][9]: orderNo2[2][10]: orderNo2[2][11]: orderNo2[2][12]: orderNo2[2][13]: orderNo2[2][14]:
After $i = 1, the later ones are all empty. I don't know why the inner for loop does not work after the 1st outer for loop iteration

Store specific row count with array_push

I'm trying to store a count of specific array items so that I know which row to style in laravel excel
What I would like to do is increase my iterator $rowCount any time I do an array_push, but any time I specifically process an array_push for $groupItem, I want to store the count in $boldRows that way I can apply styling to only those rows.
$allgroupResult= array();
$rowCount = 2; //since I have a sticky header in the excel file I start the count at 2
$boldRows = array();
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_description"] = $group->category_description;
array_push($allgroupResult, $groupItem);
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->info->identifier;
array_push($allgroupResult, $skuItem);
}
}
category one with 3 products (total of 4 rows) and category two with 2 products (total of 3 rows), would give me 7 total rows starting at row 2. So my expected result with this would be that $boldRows would then contain 2 and 6 for the category rows (because my count starts at 2 then processes 3 products so the next category row is at 6)
How can I properly achieve this?
I would have thought that you just needed to increment the rowcount each time you push a new element to the array and keep track of the rows you want to be in bold...
$rowCount = 2; //since I have a sticky header in the excel file I start the count at 2
$boldRows = array();
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_description"] = $group->category_description;
array_push($allgroupResult, $groupItem);
array_push($boldRows, $rowCount++); // Store count & increment
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->info->identifier;
array_push($allgroupResult, $skuItem);
$rowCount++; // Just increment count
}
}
You may need to adjust the $rowCount depending on how the rows match the arrays - arrays are 0 based and I don't know how the rows will be based.
Based on PHPExcel Make first row bold and the row number excel row conversion from Convert A to 1 B to 2 ... Z to 26 and then AA to 27 AB to 28 (column indexes to column references in Excel) (modified for PHP), you can then use something like ...
foreach ( $boldRows as $row ) {
$cell_name = excelColumnFromNumber($row)."1";
$objPHPExcel->getActiveSheet()->getStyle( $cell_name )->getFont()->setBold( true );
}
function excelColumnFromNumber($column)
{
$columnString = "";
while ($column > 0)
{
$currentLetterNumber = ($column - 1) % 26;
$columnString = chr($currentLetterNumber + 65) . $columnString;
$column = ($column - ($currentLetterNumber + 1)) / 26;
}
return $columnString;
}

While loop ommitting every fourth result

I'm try to dynamically generate an HTML table containing data from a database. The output looks perfect, but upon closer inspection I realized it was omitting every fourth result. I know it has something to do with the structure of my while loop and the if/else statement within, but I'm not sure what it is exactly.
$i=0;
while ($person = $pull_person->fetch()){
if ($i <= 2){
echo "<td valign='top'>";
echo "<h3>" . $person['person_name'] . " - " . $person['person_id'] . "</h3>";
echo "<label style='background-image:url(" . $person['person_pic'] . ");'><input type='checkbox' name='person[]' value='" . $person['person_id'] . "''></label>";
echo "</td>";
$i++;
}
else{
echo "</tr>";
echo "<tr>";
$i=0;
}
}
It's gotta be something simple/obvious, but it's not registering with me. Thanks!
The loop is not hitting the fourth result because of the loop limiting logic.
$i=0;
while ($person = $pull_person->fetch()){
if ($i <= 2){
echo "<p>item: $i</p>";
$i++;
}
}
Iteration 1: $i = 0
Iteration 2: $i = 1
Iteration 3: $i = 2
Iteration 4: $i = 3
Iteration 4 is never hit because it checks and sees that $i must be less than or equal to 2. If you change this to be less than or equal to 3 it will work as you want.
if ($i <= 3)
Evidently... you only increment the variable $i when the condition is met:
$i=0;
while ($person = $pull_person->fetch())
{
if ($i <= 2)
{
//output
$i++;
}
else
{
//no output
$i=0;
}
}
So this happens:
iteration old $i new $i output
1 0 1 yes
2 1 2 yes
3 2 3 yes
4 3 0 no //condition not met
5 0 1 yes //loops...
...
What you observe here is that the code will skip the output of the iteration given by the number in the conditional plust 2. So, for example, if you use the condtion $i <= 3, the results are:
iteration old $i new $i output
1 0 1 yes
2 1 2 yes
3 2 3 yes
4 3 4 yes
5 4 0 no //condition not met
6 0 1 yes //loops...
...
If you want to insert something each n iterations, do as follows:
$n = 3; //number of items per row
$i = 0;
while ($person = $pull_person->fetch())
{
//output item
$i++;
if ($i == $n)
{
//something each $n iterations
$i=0;
}
}
The effect is the following (assuming $n = 3):
iteration old $i new $i new row
1 0 1 no
2 1 2 no
3 2 0 yes //condition is met, $i reset to 0
4 0 1 no
5 1 2 no
6 2 0 yes //condition is met, $i reset to 0
...
Note 1: every iteration outputs an item.
Note 2: you can adjust the initial value of i to have an offset.
Misread question but will leave the answer as it may provide use anyway.
Best way is to look at the array as a 'module' (mathematically) -- i.e. use Modular Arithmetic:
if ((i % 4) == 0)
That is to say, indices 0, 4, 8, 12, 16, ... will be targeted, only. And, actually, this is how most setInterval functions work under the hood.

PHP Nested While loop not working with mysql_fetch_assoc

I tested looping nested While statements so:
$count1 = 0;
while ($count1 < 3) {
$count1++;
$count2 = 0;
echo "count1: ".$count1."<br />";
while ($count2 < 3) {
$count2++;
echo "count2: ".$count2."<br />";
}
}
This works perfectly (looping three times each) with results:
count1: 1
count2: 1
count2: 2
count2: 3
count1: 2
count2: 1
count2: 2
count2: 3
count1: 3
count2: 1
count2: 2
count2: 3
Then I tried the same with a loop using mysql_fetch_assoc ($ContactsInterests is a two row associative array, and $LatestNews has 50 rows) i.e.
$CI_count = 0;
while ($CI_Row = mysql_fetch_assoc($ContactsInterests)) { //loop thru interests
$CI_count++;
$LN_count = 0;
echo "CI_count: ".$CI_count."<br />";
while ($LN_Row = mysql_fetch_assoc($LatestNews)) { //loop thru news
$LN_count++;
echo "LN_count: ".$LN_count."<br />";
}
}
The results are:
CI_count: 1
LN_count: 1
LN_count: 2
...
LN_count: 50
LN_count: 51
CI_count: 2
But where it the second iteration of LN_count? I don't understand why the LN_count didn't increment a second time.
Help appreciated.
mysql_fetch_assoc does iteration for "mysql result" type. Seeks index for each fetch.
you must use mysql_data_seek to go to the first result like;
<?php
$CI_count = 0;
while ($CI_Row = mysql_fetch_assoc($ContactsInterests)) { //loop thru interests
$CI_count++;
$LN_count = 0;
echo "CI_count: ".$CI_count."<br />";
mysql_data_seek($LatestNews,0);
while ($LN_Row = mysql_fetch_assoc($LatestNews)) { //loop thru news
$LN_count++;
echo "LN_count: ".$LN_count."<br />";
}
}
Because the results have been exhausted. You've iterated through all of them... If you wanted to loop again you're have to repopulate the $LatestNews variable.
You need to reset the mysql internal pointer:
see http://php.net/mysql_data_seek
mysql_fetch_assoc() takes out one by one the rows of the source, you when you will be in the second step of the first loop, you wont have any rows in the source variable.
You need to put the results of the second query in an array, then loop the array, and not using mysql_fetch_assoc.

Categories