mysqli_fetch_array stop / multiple usage - php

I tried to use mysqli_fetch_array twice in a code, and it seems not to work.
<?php
$i=1;
while ($i < $x)
{
while($data=mysqli_fetch_array($result))
{
echo $i;
echo $data['ID'];
}
}
while {$i > $x)
{
while($data=mysqli_fetch_array($result))
{
echo $i;
echo $data['ID2']
}
}
?>
This doesn't work for the second case. Why? Is there any way to get my idea to work?
I want to use some of the array values in one place, and some in another

After you fetch all rows the pointer is at the end of the result set and you cannot fetch more. You need to reset it to the first row before attempting to fetch from it again:
mysqli_data_seek($result, 0);
Alternately, the first time through assign rows to an array and use that later:
while($data=mysqli_fetch_array($result)) {
$rows[] = $data;
}
foreach($rows as $row) {
//whatever
}
Depending on what you are doing not a great way to split up rows, but based on your comments (you have to increment your counter):
$i=1;
while($data=mysqli_fetch_array($result) && $i < 11) {
echo $data['ID'];
$i++;
}
while($data=mysqli_fetch_array($result) && $i < 21) {
echo $data['ID2'];
$i++;
}

Where are you resetting the vars, also you should do the while loop like this:
while ( ($i < $x) || ($data=mysqli_fetch_array($result) ) )

Related

How to generate random number 5 times in php using do while loop

I want the function to generate random number 5 times. I have already tried to use do/while loop but it doesn't work:
function myfunction(): int {
$i = 0;
do {
$k = mt_rand(10000, 99999);
$i++;
} while($i <= 4);
{
return $k;
}
}
Stack your values in an array, inside your do-while loop and just before your function ends add the return of the array.
Something like this :
function myfunction():array{
$randomNumbers = array();
$i=0;
do{
$k=mt_rand(10000,99999);
$i++;
$randomNumbers[] = $k;
} while($i<=4) ;
return $randomNumbers;
}
print_r(myfunction());
The syntax of do while loop is given below...
do {
code to be executed;
} while (condition is true);
You have to update your code as it is given below...
do{
$k=mt_rand(10000,99999);
$i++;
}while($i<=4);
I have modified the code a little bit and tested it before sending it to you . Please check the following code :-
function fiveRandomNos(){
$rno = array();
$count=0;
while($count <= 4){
$rno[]=mt_rand(10000,99999);
$count++;
}
return $rno;
}
print_r(fiveRandomNos()) ;

Display even & odd numbers

I am trying loop through an array of random numbers if the the number is dividable by two then it's even and I then want to assign this to an array $even[], and if odd then assign it to the odd array. I have managed to display the results without using an array but for the sake of this I want to put them into their own array. However I can't seem to get this result I'm after all I get is this error: message Array to string conversion.
<?php
$numbers = array();
for ($i=0; $i<=1000; $i++) {
$numbers[]=mt_rand(1,1000);
if ($i % 2 == 0){
$even[]=$i;
} else {
$odd[]=$i;
}
}
echo $even;
echo $odd;
?>
Try this to echo the results.
foreach ($even as $evens){
echo $evens . '<br/>';
}
define $odd and $even as a array
$even = array();
$odd = array();
check if($i%2 == 0)
{
$even[] = $i;
}
else
{
$odd = $i;
}
var_dump($even);
var_dump($odd);

Putting Incremental values in array

I have the following code
<?php
for ($i=1; $i<=4; $i++)
{
echo $i . "<br>";
}
?>
which Results 1 2 3 4
I have another code which have a variable $number = 1 and coding is as follows
if ($number = $i)
{
echo "Message" ;
}
MY issue is i need to check the variable $number(1) is available in the list of values displayed by variable $i.
Can anybody find a solution for that? Thanks in advance .
You should use == (comparison operator) instead of = to compare two values within an if statement, as follows:
if ($number == $i)
{
echo "Message" ;
}
However, assuming that it's just a typo, here is my suggestion:
You can store the numbers to be printed in an array first.
$i = array(1,2,3,4);
foreach($i as $num)
{
echo $num."<br/>" ;
}
Then, you can use the in_array() function of php to check if $number is present in any of the numbers printed above.
if(in_array($number, $i))
{
echo "Message" ;
}
try this this will put your incremental values in array and after that you can validate with any number
$inc=array();
for ($i=1; $i<=4; $i++)
{
array_push($inc,$i);
}
$number=1;
if(in_array($number, $inc))
{
echo "your Message" ;
}
= is the assigment operator, to compare you have to use ==
if ($number == 1) {
//...
}
Furthermore there are situatons where using
if ($number = $value) {
//...
}
is of use. this code would assign $value to $number and 'return' its value, therefore if ($number = 1) echo "hi"; will echo "hi" - while `if ($number = 0) echo "hi"; will output nothing. A assign like the above is quite useless, it is mostly used when you call a function, you want to restore its return value and compare it in the same step like
if ($number = getValue()) {
//$number is set to getValue() and is not 0, null or false
}

Insert tr after every third loop

I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.
Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.
You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.
$i=0;
while(guard()){
if($i % 3 == 0){
//ploing
}
$i++
}
This code will close any extra rows:
<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
//if this is first value in row, create new row
if ($i % $columns == 1) {
echo "<tr>";
}
echo "<td>".$row[0]."</td>";
//if this is last value in row, end row
if ($i % $columns == 0) {
echo "</tr>";
}
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
for ($j=1; $j<=$spacercells; $j++) {
echo "<td></td>";
}
echo "</tr>";
}
?>
</table>
I haven't tested the code, but the logic should work:
<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
if($i == 0){
echo"<TR>";
}
echo"<td>".$row[0]."<TD>";
$i++;
if($i == 3)
{
$i = 0;
echo"</tr>"
}
}
if($i ==1){
echo "<td></td><td></td></tr>";
}
if($i ==2)
{
echo "<td></td></tr>";
}
?>
<table>

Limit the number of items in an array

I have an array that gets processed through a foreach loop.
foreach($images as $imageChoices) {
Do stuff here
}
How do I limit the loop to only process the first 4 items in the array?
The array_slice() function can be used.
foreach(array_slice($images, 0, 4) as $imageChoices) { … }
This enables you to only loop over the values that you want, without keeping count of how many you have done so far.
You basically count each iteration with the $i and stop the loop with break when 4 is reached...
$i = 0;
foreach($images as $imageChoices) {
//Do stuff here
$i++;
if($i >= 4 { break; }
}
You can do:
for($i=0; $i<count($images) && $i<4; $i++) {
// Use $images[$i]
}
Use a counter variable and increase it's count on each loop.
Apply check according to counter value
something like below:
$count=1;
foreach($images as $imageChoices) {
if($count <=4)
{
Do stuff here
}
else
{
Jump outside of loop break;
}
$count++;
}
OR you can do same with for loop instead of foreach and also with some inbuilt PHP Array functions
for($i=0; $i<4; $i++) {
// Use $images[$i]
}
function process()
{
//Some stuff
}
process(current($imageChoices));
process(next($imageChoices));
process(next($imageChoices));
process(next($imageChoices));

Categories