Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My client is using PHP version 5.2.0, and I have some code that works perfectly, but uses 5.4.7 at the moment. When my code is run on their server, I receive a parse error. I've searched online for an alternative way to write this code, but I can't find anything. If anyone has any tips that would be a great help.
**$count = mysqli_query($con, "SELECT COUNT(*) FROM menuitem")->fetch_row()[0];** //I want to make sure the previous and next button are only displayed exactly to the number of items
//in my database. I have to count those items, and turn that mysqli_query object into data. fetch_row[0] stores them
//in an array, starting at 0, turning it into an integer I can use to count below.
$prev = $start - 3;
if ($prev >= 0) {
echo '<td>Previous Items</td>';// Set your $start - 3 since our offset is 3, and pass that to a variable.
} //if $start - 3 is still greater than 0, then display the previous button. Logically, that is page 2.
$next = $start + 3;
if ($next < $count) {
echo '<td>Next Items</td>';// Since i used fetch_row[0] I now know how many rows I have. If $start + 3 is less than the $count query
} // than you will have a next button. If that number exceeds the number of rows I have, then no next button.
echo "</tr>";
echo "</table>";
In the code above, the ...->fetch_row()[0]; is the section that is bringing back the error. PHP 5.2.0 does not like it.
Prior to PHP 5.4, you could not dereference arrays returned from function calls. Simply do:
$count = mysqli_query(...)->fetch_row();
$count = $count[0];
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please am looking for a code or php function that can help me know how many 5 is in 25
the code below only returns 5,10,15,20
i want something that will tell 4 instead of breaking the division point
thanks in advance
for($i = 1; $i < 25; $i++)
{
if ($i % 5 == 0)
{
echo $i;
}
};
The reason is you limit your loop to stop when it hits 25. It will only loop when value is less than 25, so as soon as it reaches 25 it wont loop and so wont get your final set of 5s.
Try this:
for ($i = 1; $i <= 25; $i++)
{
if ($i % 5 == 0) {
echo $i;
}
};
isn't this just a simple division (25%5=5)?
the reason why your code gives your 4 is because you are actually not getting to 25 (the loop stops when i=25), if you will usefor($i=0;$i<=25;$i++) the loop will still run when I=25, and so it will show you 5.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
so I am trying to check if a string contains a specific set of words, and set a variable if they do. I have the code below, first it was working great except that it would not work if one of the key words was the very first word, so I looked at the php manual and found out about === and implemented that, but now it sets the variable to one every time even if none of the words are found!
So basically it reads a text file to an array, the text file contains the key words, then it checks the string to see if any of those key words are present in the sting. If none of the key words are found then wc would equal 0 and so would inc. If it finds any then wc is incremented every time a word is found, and if it is greater than 0 it will set inc to 1 to flag that key words were included.
Hopefully that all makes sense....
Here is my code:
$inc = 0;
$list = file("filter.txt", FILE_IGNORE_NEW_LINES);
$cnt = count($list);
$wc = 0;
for ($i=0; $i<$cnt; $i++)
{
if (strpos($string,$list[$i]) === false)
{
$wc ++;
}
if ($wc > 0)
{
$inc = 1;
}
}
It doesn't work because you are increasing wc if the string is not found.
You have to replace === with !==.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So I am trying to do the following.
I have an array that contains 12 numbers(months).
$array = array();
$array[1] = array();
and so on.
Now, I am trying to push numbers into second place like
$array[5][day should be pushed here] trying like so
$counter = 0;
while($counter <= $elements)
{
if($something == $something)
{
$startfrom = 14; //example
$month = 5; //example
while($startfrom <= 20)
{
array_push($array[$month], $startfrom );
$startfrom ++;
}
}
But its always returning an error. Like this
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in F:\xampp\htdocs\NewPassword\Testt\process_info.php on line 27
What I am trying to achieve is that each day goes to its responding month.
Thanks.
Edit:
Nwm, fixed I did biggest rookie mistake I could. I wasn't incrementing counter out of if statement... Thank you APerson.
Should be:
$counter = 0;
while($counter <= $elements)
{
if($something == $something)
{
$startfrom = 14; //example
$month = 5; //example
while($startfrom <= 20)
{
array_push($array[$month], $startfrom );
$startfrom ++;
}
$counter++;
}
I'm going to first point out in your referenced code that there's no indication that the outermost while loop is ever terminated (or even closed properly for that matter ;) so assuming infinite recursion is not actually not the source for PHP's memory leak, I'd encourage you to first:
Grab a solid debugger like Xdebug, or (if you've the money) PHPEd so that you can profile your scripts and get detailed information about any issues you may have in your code or php settings.
If you can affirm via debugging that there's no identifiable issues with your scripts (this is almost always sure to be the problem though), then you can simply increase the memory_limit in your php.ini file. I'd be weary of going to high here. If you have to surpass 512M then something is seriously wrong.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here I am rephrasing the question. Coz some of the people couldnt understand it and started giving negative votes
I have the following PHP code:
$count = 100000;
$array = array();
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
As per current code the time to execute this code will increase as I increase $count.
I am looking for a solution / algo / technique by which I can keep the time to execute in seconds no matter whatever is the value for $count. I am running this on 8 cores cpu, I am OK changing language but not ok upgrading hardware.
This should give you approximately constant time for values of $count < $maxcount. However, I wouldn't ever do this - it is just timewasting.
$maxcount = 100000
$count = 100000;
$array = array();
// Create required array
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
// Pad out time to $maxcount iterations, to keep time constant
for($i=$count,$i<=$maxcount,$i++)
{
$dummy = mt_rand(1111111111111111,9999999999999999);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I skip a loop and go to the next one? Lets say my query in a loop returns result 'Nothing', I want to skip updating the base and go to the next loop. If I have 7 loops and my 5th loop returns 'Nothing' from my base, skip it don't do nothing beyond that point and just go to the next, 6th loop.
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
if ($name = 'Nothing')
{
go to next loop;
}
updating my database and stuff;
}
EDIT: I have a $_SESSION variable at the end that stores all of my loops and displays them. I also don't want results with 'Nothing' to be stored there.
continue will end the current iteration and start the next one:
if ($name == 'Nothing')
// -------^^ Make sure you're using double (==).
{
continue;
}