exclude array numbers for mysql - php

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']);

Related

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;
}

array conditions

I have the following array
$example=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$limit=4 // 4 at the beginning only...
//it used to get incremented automatically to 8,12,16....
At first i want 1,2,3,4as an output for which i have done
foreach($example as $eg)
{
if($eg>$limit)
continue;
}
and i am easily getting 1,2,3,4 at the first then 1,2,3,4,5,6,7,8 then1,2,3,4,5,6,7,8,9,10,11,12
But now i what i want is 1,2,3,4 at the very beginning then 5,6,7,8 then 9,10,11,12 lyk this... how can i get that???
please do help me... :)
AS the
foreach($example as $eg)
{
if($eg>$limit)
continue;
}
is returning only 1,2,3,4 at $limit=4 and 1,2,3,4,5,6,7,8 at $limit=8
i need 1,2,3,4 at $limit=4 and 5,6,7,8 at $limit=8
Have you tried using the helpful built-in functions?
array_chunk($example,$limit);
Alternatively, for more page-like behaviour:
$pagenum = 2; // change based on page
$offset = $pagenum * $limit;
array_slice($example,$offset,$limit);
You would first chunk the array so each chunk has 4 elements, then loop through each chunk:
To change the numbers shown depending on which group, you could do:
$group = $_GET['group'];
$items = array_chunk($example, ceil(count($example)/4)[$group-1];
echo implode(", ", $items);
Then you can go to
yoursite.com/page.php?group=1
And it will output
1, 2, 3, 4
And when you go to
yoursite.com/page.php?group=2
It will output
5, 6, 7, 8
etc.
You can try something like this
$example=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$limit = 8;
$limit -= 4;
for($i = $limit; $i < ($limit + 4); $i++)
{
echo $example[$i].' ';
}
Output
//for $limit 4 output 1 2 3 4
//for $limit 8 output 5 6 7 8
//for $limit 16 output 13 14 15 16

fill 2 dimensional array random in a bingo way

I've got these two functions:
function drawNumber($drawnNumbers){
$unique = true;
while ($unique == true){
$number = mt_rand(10, 69);
if (!in_array($number, $drawnNumbers)){
return $number;
$unique = false;
}
}
}
fillCard(); ?>
It's a bingo game. The card gets filled with random Numbers. But I can't get it like this:
column column column column column column
row 10 13 16 14 16 19
row 24 26 28 29 23 21
row 36 33 39 30 31 35
row 46 48 42 45 43 47
row 59 56 51 52 58 50
row 60 65 68 62 61 67
So I would like to have the first row with numbers from 10 to 19
the second row from 20 to 29 and so on.
I tried like this
<?php drawnNumber(): $number = mt_rand(0,9);
fillArray(): $number = $row . $number; ?>
But that doesn't work, because there are double numbers in the card.
So before that I tried it in a different way,with in_array:
<?php
function fillCard(){
$card = array();
/* fill card with random numbers */
for($i = 0, $min = 10, $max = 19; $i < 6; $i++, $min+=10, $max += 10)
{
for($j = 0; $j < 6; $j++)
{
$number = mt_rand($min,$max) ;
if(!in_array($number, $card){
$card['row' . $i]['column' . $j]['number'] = $number;
$card['row' . $i]['column' . $j]['found'] = 0;
}
}
}
var_dump($card);
return $card;
} ?>
But there are still double random numbers in the card.
I tried a few other thinks in the last two weeks, but I just can't get it to work together.
If one thing succeeds the other thing fails.
I can get the random numbers but not unique random numbers in the card.
I hope someone can help me.
(for extra information: it's a bingo game. So drawnNumber() are the "balls", which get drawn
and stored in the array $drawnNumbers, they also are unique random numbers. fillCard() is the
function that fills the bingo card and checks if $drawNumber is in $card)
I would appreciate some help, if someone can tell me how to get it to work. Maybe in
an algorithm way or else some code?
Thank you in advance.
In general, you draw from some kind of box, right? So do the same, have an array with all available numbers and once you get a random number out of it, remove it, so the next time you search for a number, you will only pick from the remaining ones. Small example:
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
we pick a random number between 0 and 3 inclusive (0 and the length - 1 of a that is). Let's say we picked index 2, then a will look like:
a[0] = 1
a[1] = 2
a[2] = 4
Now if you draw a number between 0 and 2 (note that you take the length - 1 of a!), you won't re-pick the already chosen number in any way thus giving you unique numbers ALL the time.
P.S. this is a simplified version, but now if you can apply that to yourself and, for your example, create several arrays you will pick from.
The simplest way would be to have an additional flat array to keep track, and loop mt_rand
Here's an example of the meat of things:
$drawn = array();
// Loop around until you have a new number in the desired range
do {
$number = mt_rand($min,$max);
} while(in_array($number, $drawn));
// And save it to the $drawn array
$drawn[] = $rand;
To clarify, the above snippet (without the initialization of $drawn) is meant to replace the line
$number = mt_rand($min,$max) ;
in your code.
define('NB_ROWS', 6);
define('NB_COLS', 6);
$rows = range(10, NB_ROWS * 10, 10);
$bingo = array();
foreach($rows as $rowIndex)
{
$availNumbers = range(0, 9);
$line = array();
for($cellIndex = 0; $cellIndex < NB_COLS; $cellIndex++)
{
// pick a random value among remaining ones for current line
$numIndex = rand(0, count($availNumbers)-1);
list($value) = array_splice($availNumbers, $numIndex, 1);
$line[] = $value + $rowIndex;
}
$bingo[] = $line;
}
print_r($bingo);

Loop through mysql table row and check if any column matches a set number in php

Im trying to loop through a mysql table and check if a row contains the number I specify:
Here is what I have:
mysql table with numbers:
mysql table:
no1|no2|no3|no4|no5
1 3 5 2 6
4 7 8 9 8
2 6 9 1 0
...
For Example: I have number
4 5 3 7
So in the first row i should get a total of 2 as there are numbers 3 and 5 first row and this numbers are in the number I have specified.
In the second row I should get a total of 1 as only a 4 is in the row and the number i have specified.
And in the last row total should be 0 as there are no matches.
I hope its clear.
I have tried the following but it dont work I hope someone can help me work it out thanks in advance.
$lottono1=4;
$lottono2=5;
$lottono3=3;
$lottono4=7;
$no1 = 0;
$no2 = 0;
$no3 = 0;
$no4 = 0;
do { ?>
// i done the following if code for each numbers but
//putting this only to take less space
if (($row_Recordset1['no1']=$lottono1) || ($row_Recordset1['no1']=$lottono2) || ($row_Recordset1['no1']=$lottono3) || ($row_Recordset1['no1']=$lottono4)) {
$no1=1;
}
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
select *,
if(no1 in (4,5,3,7),1,0)+
if(no2 in (4,5,3,7),1,0)+
if(no3 in (4,5,3,7),1,0)+
if(no4 in (4,5,3,7),1,0)+
if(no5 in (4,5,3,7),1,0) as found
from table
Well for one, your operators are wrong in your "if" conditions (you're setting rather than comparing).
Regardless i'd do something more like:
$numbers_to_match = array(4,5,3,7) ;
$query = mysql_query("select * from `table` where ____",connection_here);
$matches[] = array();
$i=0;
while($r=mysql_fetch_array($query)){
$matches[$i]=0;
foreach($r as $val){
if (in_array($val,$numbers_to_match)){
$matches[$i]++;
}
}
$i++;
}
print_r($matches);
Untested, but this should give you an array that lists the number of matches for each row
To accomplish with PHP/MySQL you can do the following:
$query = 'SELECT * FROM table';
$result = mysql_query($query) or die();
$matchValues = array(4,5,3,7);
while($row = mysql_fetch_array($result)){
$counter = 0;
foreach($matchValues as $value)
{
if(in_array($value, $row))
{
$counter++;
}
}
print "Searched row and found $counter matches<br/>";
}

(PHP) Spreading array contents equally across a fixed number of columns

I am going nuts here - hope you can help me figure this out! What is supposed to be very simple math is utterly confusing me.
$columns = 3;
$items = range(1,20);
$total = count($items);
$col1 = ???; $col2 = ???; $col3 = ???;
// $col1 must be an array of (1,2,3,4...)
// $col2 must be an array of (8,9,10,11...)
// $col3 must be an array of (15,16,17,18...)
COL1 COL2 COL3
1 8 15
2 9 16
3 10 17
4 11 18
5 12 19
6 13 20
7 14
The above is a visual example of what I am trying to achieve. Basically, for any given number of items in an array and for any given number of columns, how do I produce n number of arrays (equalling number of columns) that are as equal length as possible. If equal length is not possible (as in the example above), they must be spread out as evenly as possible and the last array must be the shortest.
Any guidance on how I construct $col1/2/3 in the example above would be much appreciated!
Please ignore the fact that I used range and integers to generate the array - this is just to simplify the example. Assume the array will contain strings.
Thanks for any help!
You do want to use array_chunk, but you need to calculate the chunk size yourself:
list($col1, $col2, $col3) = array_chunk($items, ceil($total / $columns));
I think you might want array_chunk
list($col1,$col2,$col3)=array_chunk($arrofstrings,$colcount);
$itemsInColumn = ceil($total / $columns);
$columns = 3;
$items = range(1, 20);
$rows = array_chunk($items, $columns);
$columns = array();
foreach ($rows as $row) {
for ($i = 0; $i < $columns; $i++) {
$columns[$i][] = $row[$i];
}
}

Categories