I have following code:
while($row = mysql_fetch_assoc($result))
{
if(strtolower($message) == $row['question'])
{
msg($row['answer']);
update($row['question']);
} else {
dodaj(strtolower($message), '', 0);
}
}
and I would like to execute function dodaj(); only one time, not a few times. How to do it? Please help, thank you very much.
Greetings.
Use a variable to remember if you called dodaj.
$called_dodaj = false;
while($row = mysql_fetch_assoc($result))
{
if(strtolower($message) == $row['question'])
{
msg($row['answer']);
update($row['question']);
} elseif (!$called_dodaj) {
dodaj(strtolower($message), '', 0);
$called_dodaj = true;
}
}
Use a break statement:
while($row = mysql_fetch_assoc($result))
{
if(strtolower($message) == $row['question'])
{
msg($row['answer']);
update($row['question']);
} else {
dodaj(strtolower($message), '', 0);
break;
}
}
EDIT: I assumed you meant to stop processing results once dodaj() was called. However, if you mean that you only want to call dodaj() one time, use a flag variable:
$dodajCalled = false;
while($row = mysql_fetch_assoc($result))
{
if(strtolower($message) == $row['question'])
{
msg($row['answer']);
update($row['question']);
} elseif (!$dodajCalled) {
dodaj(strtolower($message), '', 0);
$dodajCalled = true;
}
}
Related
Im sending a mail from one function to another like this
sendMail($email)
if($result == 1) {
return redirect('/')->with("msg", $response);
} else {
return redirect('/')->with("msg", $badResponse);
}
function sendMail($email) {
//...
if($mail->send){
$result = 1;
} else {
$result = 2;
}
echo $result;
}
How do i get the value of $result and use it after the function call?
You need to return the result and save it to a variable, rather than just echoing it:
$result = sendMail($email);
if($result == 1) {
return redirect('/')->with("msg", $response);
} else {
return redirect('/')->with("msg", $badResponse);
}
function sendMail($email) {
//...
if($mail->send){
$result = 1;
} else {
$result = 2;
}
return $result;
}
I need help to check if variables (from a database) are the same, like a test.
First I was using just "==", but I saw this: http://php.net/manual/en/language.operators.comparison.php , and now I'm using "===". But it still doesn't work.
My code:
$uids = array();
$trues = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($uids, $row['UID']);
} while ($row1 = mysqli_fetch_assoc($result1)) {
array_push($uids, $row1['UID']);
} while ($row2 = mysqli_fetch_assoc($result2)) {
array_push($uids, $row2['UID']);
}
for ($i = 0; $i < count($uids); $i++) {
$r = 0;
if ($row['question1'] === $row['uAnswer1']) {
$r++;
} else {
$r = $r;
} if ($row['question2'] === $row['uAnswer2']) {
$r++;
} else {
$r = $r;
} if ($row['question3'] === $row['uAnswer3']) {
$r++;
} else {
$r = $r;
} if ($row['question4'] === $row['uAnswer4']) {
$r++;
} else {
$r = $r;
} if ($row['question5'] === $row['uAnswer5']) {
$r++;
} else {
$r = $r;
} if ($row['question6'] === $row['uAnswer6']) {
$r++;
} else {
$r = $r;
} if ($row['question7'] === $row['uAnswer7']) {
$r++;
} else {
$r = $r;
} if ($row['question8'] === $row['uAnswer8']) {
$r++;
} else {
$r = $r;
} if ($row['question9'] === $row['uAnswer9']) {
$r++;
} else {
$r = $r;
} if ($row['question10'] === $row['uAnswer10']) {
$r++;
} else {
$r = $r;
}
array_push($trues, $r);
echo $uids[$i] . " [" . $r . "]<br>";
}
print_r($trues);
}
What the result is: https://hastebin.com/uxitoyoyib.php
So it actually says everything is right, but I know that it isn't. Can you help me with this?
Thanks!
Compare variables like
if ( strtolower(trim($row['question5'])) === strtolower(trim($row['uAnswer5'])) )
Remove spaces if added with strings, convert the string in lowercase to the exact match of both strings.
use
if ($row['question4'] == $row['uAnswer4']) {
//do something
}
and try to print your 2 variable.
You can use the strcmp() or strcasecmp() functions or the === operator.
I wrote this function for caching queries:
function get_cached($query, $type = 1) {
$key = md5(strtolower($query));
$cached = $memcache->get($key);
if($cached) {
$res = unserialize($cached);
} else {
$r = mysql_query($query);
if($mode == 1) {
$res = mysql_fetch_object($r);
} else {
$res = mysql_fetch_array($r);
}
$memcache->store($key, serialize($res));
}
return $res;
}
And it kinda works, i can cache simple queries. But i have problem with queries that operate within loops.
I really can't understand how to get around this, and "port" this to use my get_cached() function first.
$ref = mysql_query("query");
while($res = mysql_fetch_object($ref)) {
//do something with results
}
How i can do it through cache?
You cannot store the mysql result object but you can fetch all rows into an array and cache the array. like
function get_cached($query, $type = 1) {
$key = md5(strtolower($query) . $type);
$cached = $memcache->get($key);
if($cached) {
return unserialize($cached);
} else {
$r = mysql_query($query);
$data = array();
if($mode == 1) {
foreach($row = mysql_fetch_object($r)) {
$data[] = $row;
}
} else {
foreach($row = mysql_fetch_array($r)) {
$data[] = $row;
}
}
$memcache->store($key, serialize($data));
}
return $data;
}
don't forget to store the type of your functions as cache key too, otherwise you fetch a results set with type==1 and in return you get the elements as array
Hello I have this sorting function ine one template of CMS. I would like to add if the condition is the second one:
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
I want to add the following MySQL result to show also
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
which is used in this template for sorting:
include($basepath.'/templates/template.channel_item_title.php');
Any ideas how this can be integrated inside the first code function?
So here is the code of the main index template:
<?
session_start();
if(($_REQUEST[mode] == 'favorites' || $_REQUEST[mode] == 'my_uploads') && !$_SESSION[username]) {
header("Location: /login.php");
}
include('admin/db.php');
include($basepath.'/includes/inc.seo.php');
$cacheName = $_SERVER[REQUEST_URI];
$cacheResult = getCache($cacheName);
$cacheTotalPages = getCache("total_pages$cacheName");
if($cacheResult && $cacheTotalPages) {
$array = $cacheResult;
$total_pages = $cacheTotalPages;
}
else {
include($basepath.'/includes/inc.index_queries.php');
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
if($thispage != 'favorites' && $_GET[mode] != 'my_uploads') {
setCache($cacheName,$array,$overall_cache_time);
setCache("total_pages$cacheName",$total_pages,$overall_cache_time);
}
}
$thisfile = 'index';
$webpage="index";
if($isMobile) {
include($basepath.'/templates/mobile.overall_header.php');
}
else {
include($basepath.'/templates/template.overall_header.php');
}
if(empty($_GET[mode]) && !$_GET[page]) {
include($basepath.'/templates/template.home.php');
}
if($webpage=="index" && empty($_GET[mode]) && !$_GET[page])
{}
else
{
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
}
else {
if($isMobile) {
include($basepath.'/templates/mobile.content_item.php');
}
else
{
include($basepath.'/templates/template.content_item.php');
}
}
}
}
else {
echo "Sorry, no results were found.";
}
}
if($isMobile) {
include($basepath.'/templates/mobile.overall_footer.php');
}
else {
include($basepath.'/templates/template.overall_footer.php');
}
?>
I don't think I get your question, but we have to start somewhere...
<?php
if(is_array($array)) {
foreach($array as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.content_item_photo.php');
}
Next block of code:
<?php
$result = mysql_query("SELECT niches.name, niches.record_num FROM niches ORDER BY name ASC");
if(is_array(result)) {
foreach($result as $row) {
if($row[photos] == 1) {
if($isMobile) {
include($basepath.'/templates/mobile.content_item_photo.php');
}
else
{
include($basepath.'/templates/template.channel_item_title.php');
}
function nonrecgen($min, $max, $amount) {
for($i=0;$i<$amount;$i++) {
$NrArray[$i] = rand($min,$max);
echo $NrArray[$i];
do {
for($j=0;$j<=$i;$j++) {
if ($NrArray[$j] == $NrArray[$i]) {
$NrArray[$i] = rand($min,$max); }
}
$Reccuring = false;
if ($i > 0) {
for($k=0;$k<=$i;$k++) {
if ($NrArray[$k] == $NrArray[$i]) {
$Reccuring = true; }
}
}
}
while ($Reccuring = true);
}
Return $NrArray;
}
$Test = nonrecgen(0,1,2);
print_r($Test);
I wanted to look into how to generate an array of nonreccuring numbers and while this is certainly not the most efficient way I believe, I can't seem to figure out why it loops endlessly on the first iteration. I tried logical analysis over and over, but there has to be something I'm missing.
do {
...
} while ($Reccuring = true);
Because your while statement sets $Reccuring to true, instead of evaluating it.
Try:
do {
...
} while ($Reccuring === true);
Other than the = to == you were also resetting the $Recurring in the wrong place:
<?
function nonrecgen($min, $max, $amount)
{
for($i=0;$i<$amount;$i++)
{
$NrArray[$i] = rand($min,$max);
do
{
for($j=0;$j<=$i;$j++)
{
if ($NrArray[$j] == $NrArray[$i])
{
$NrArray[$i] = rand($min,$max);
}
}
if ($i > 0)
{
for($k=0;$k<=$i;$k++)
{
if ($NrArray[$k] == $NrArray[$i])
{
$Reccuring = true;
}
}
}
$Reccuring = false;
}
while ($Reccuring == true);
}
return $NrArray;
}
$Test = nonrecgen(0,2,5);
echo "<pre>";
print_r($Test);
?>
You're currently assigning a value rather than checking (which will always be true).
Change it to: while ($Reccuring == true);