I'm having a little bit of trouble with my PHP function. In the database I get 2 results back from my query. But my function is doing something else. It changes when I edit '=' to '=='
MySQL Query:
SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName
FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID
Result:
ContentPagesID ContentTypeName
01425d4a-2abd-11e4-b991-525400 products01
014269dd-2abd-11e4-b991-525400 information01
PHP Function:
function GetAllPages() {
$GetAllPagesSql = "SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID";
$GetAllPagesQuery = mysql_query($GetAllPagesSql);
while (($GetAllPagesRow = \mysql_fetch_array($GetAllPagesQuery)) != false) {
if($GetAllPagesRow[ContentTypeName] === 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
DisplayInformation01DesignFunction();
}
}
}
When I change:
if($GetAllPagesRow[ContentTypeName] = 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] = 'information01') {
DisplayInformation01DesignFunction();
}
To this:
if($GetAllPagesRow[ContentTypeName] === 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
DisplayInformation01DesignFunction();
}
It goes from showing function DisplayProducts01DesignFunction() twice to DisplayInformation01DesignFunction() once.
Any ideas on how to fix this?
It should actually be running it only once if your code is fixed. When you change that == to = that then doesn't remain a comparison and becomes an assignment which results to TRUE and hence your if block gets executed, which is wrong.
if($GetAllPagesRow[ContentTypeName] = 'product01')
Is not the correct way to write an if condition, if you are making your code run fine using that logic then you're doing it wrong. That = should be ==
When you say
if($GetAllPagesRow[ContentTypeName] = 'product01')
Then that if block will always be executed even if you have 100 rows.
Now you might ask ok when I put == then why doesn't the first if block work anymore? Its because you are comparing a wrong string, your database contains products01 and your if contains product01, see the missing s. Your actual if condition should be
if($GetAllPagesRow[ContentTypeName] == 'products01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
DisplayInformation01DesignFunction();
}
Related
I need to check the words received from the database with the user's entered word and if there is a match, then output its value from the database, and if not, then output what the user entered.
The code below works fine if there is a match.
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
}
}
}
}
I'm an amateur in PHP, just learning. And I can't figure out how to output $d_typeplace if no match is found.
I tried to add
else {
echo $d_typeplace;
}
, but I get an array of words from the user entered.
I will be grateful for any help. Also for any suggestions for improving this code.
---Addition---
I apologize for my English. This is a problem in the Russian language, I need to take into account the morphology. To do this, the database has a list of words and their analog, for example, X = Y. I get these words and compare what the user entered. If he entered X, then we output Y. If he led Z, which is not in the database, then we output Z.
Thus, we check $d_typeplace with $d_typeplace_raw and if there is a match, we output $d_typeplace_morf, which is equal to $d_typeplace_raw. And if not, then $d_typeplace (it contains the value that the user entered).
Oh, I'm sorry, I understand myself that I'm explaining stupidly)
I cannot quite understand what you are asking: you need to output the string entered by the user, but you can only print an array?
If this is the case, I think you parsed the string before, in order to therefore you need to do join again the values contained in the array.
Try with:
else {
echo implode(" ", $d_typeplace);
}
--- EDITED ---
Try with:
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
$found = false;
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
$found = true;
break;
}
}
if (!$found) {
echo $d_typeplace;
}
}
}
But I think it would be more efficient, if you implemented the second code snippet written by #Luke.T
I'm presuming you were trying to add the else like this?
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
} else {
echo $d_typeplace;
}
}
}
}
Which was outputting an array because the for loop was continuing, if you add a break like so...
echo $d_typeplace;
break;
It should stop outputting an array. Depending on your use case you could however perform similar functionality directly in your sql query using LIKE ...
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('
SELECT ego_slovoforma_v_predlozhnom_padezhe
FROM dEzpra_jet_cct_tip_mest_obrabotki
WHERE vozmozhnyi_variant_mesta LIKE %' . $d_typeplace . '%');
if ($typeplace_results) {
//Echo result
} else {
echo $d_typeplace;
}
}
This should be simple but for some reason code in my if block is executing despite the fact that it resolves to false and it's making me very unhappy... My user_id in this case is 2.
$note = Notification::where("user_id",Auth::user()->id)->first();
$wall = $note->pluck('wall');
if($wall != 0)
{
//This code is executing!
}
else{
array_push($data,"Your First Time!");
//This code is not!
}
As you can see, my $wall should be zero so I don't understand why $wall != 0 runs.
Remove pluck
$note = Notification::where("user_id",Auth::user()->id)->first();
$wall = $note->wall; //This changed
if($wall != 0)
{
//This code is executing!
}
else{
array_push($data,"Your First Time!");
//This code is not!
}
I made the script to do what is expected, so it work ok but there must be a more elegant way to achieve the same result. I know that using switch will make it look nicer but not sure if the result will be the same as the 'default:' behavior:
This is the section of the script i want to refactor:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
// Here i call a function that do a mysql query and
// return the user active tasks
$result = tasks($deadline,$user);
$row_task = mysql_fetch_array($result);
// HERE IS THE UGLY PART <<<<<----------------
// the array will return a list of tasks where this current
// users involved, in some cases it may show active tasks
// for other users as the same task may be divided between
// users, like i start the task and you continue it, so for
// the records, user 1 and 2 are involved in the same task.
// The elseif conditions are to extract the info related
// to the current $user so if no condition apply i need
// to change function to return only unnasigned tasks.
// so the i need the first section of the elseif with the
// same conditions of the second section, that is where i
// actually take actions, just to be able to change of
// change of function in case no condition apply and insert
// tasks that are unassigned.
if ($row_task['condition1'] == 1 && etc...) {
} else if ($row_task['condition2'] == 1 && etc...) {
} else if ($row_task['condition3'] == 1 && etc...) {
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
// in case no condition found i change function
// and overwrite the variables
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition3'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
echo 'nothing to insert</br>';
}
}
}
Basically i run the else if block twice just to be able to change of function in case nothing is found in the first loop and be able to allocate records unassigned.
I haven't changed the functionality of your code, but this is definitely a lot cleaner.
The main problem was that your logic for your if/else statements was confused. When you're writing:
if($a == 1){ } else if($b == 1){ } else if($c == 1){ }else{ //do something }
You're saying If a is 1 do nothing, if b is 1 do nothing, if c is 1 do nothing, but if all of those did nothing, do something when you can just say if a is not 1 and b is not 1 and c is not 1, do something.
I wasn't too sure on your second if statements, but generally it's not good to have an if else with no body within it. However, if the "insert into database" comment does the same thing, you can merge the 3 if statements that do the same code.
I hope i've cleared a few things up for you.
Here's what I ended up with:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
$result = tasks($deadline, $user);
$row_task = mysql_fetch_array($result);
if (!($row_task['condition1'] == 1 || $row_task['condition2'] == 1 || $row_task['condition3'] == 1 || $row_task['condition4'] == 1)) {
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1) {
// insert into database
} else if ($row_task['condition3'] == 1) {
// insert into database
} else if ($row_task['condition4'] == 1) {
} else {
echo 'nothing to insert</br>';
}
}
}
I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.
Here is my code:
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode($fbaddra);
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['addr'];
} else {
$fbaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['addr'];
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
UPDATE: The GET Request
I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.
domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename
The GET above currently returns null.
Requests that work:
domain.com/api/core/engine.php?a=fbaddra&facebook=username or domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename
These requests return the full array with the values that match, or null for the values that don't.
TL;DR
I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.
rather than assigning as 'null' assign null. Your full code as follows :
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode('no match');
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
You can even leave else part altogether.
Check your code in this fragment you not use same names for variables:
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = 'null';
}
$fbaddr not is same as $fpaddr, this assign wrong result to if statement.
It was the mysql query that was the problem.
For those who come across this, and need something similar, you'll need to format your query like this:
** MYSQL QUERY **
if ($_GET['PUTVALUEHERE']) {
$g = $_GET['PUTVALUEHERE'];
$gq = $mysqli->real_escape_string($g);
$q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}
** PHP CODE **
if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);
}
Why won't my search function ever execute the "else" (else should echo a text when no resulsts haved been found)? I also have some problems when trying to show all results (with no search criterias selected, just by pressing the search button). I'll upload the whole code of the page because I don't know if you need the HTML part as well or not to figure out the problem. I know it's a big chunk of code but please help out if you can. Thanks!
Here's a link to my code: http://pastebin.com/BXe1C0dr
This is not yet the answer, just a brief code structure of Matei Panchios code. Because it is hard to make sense of long code, so I try to simplify it so that other people might be able to help.
$termeni = mysql_real_escape_string($_POST['termeni']);
$tip=$_POST['tip'];
$judet=$_POST['judet'];
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
$query = "SELECT * FROM oferte WHERE localitate LIKE '%$termeni%' AND
tip_locatie='$tip' AND judet='$judet'";
// do the query and write some HTML
} elseif (isset($tip)) {
$query = "SELECT * FROM oferte WHERE tip_locatie='$tip'";
// do the query and write some HTML
} elseif (isset($judet)) {
$query = "SELECT * FROM oferte WHERE judet='$judet'";
// do the query and write some HTML
} elseif (!empty($termeni)) {
...
} elseif (!empty($termeni) AND (isset($judet))) {
...
} elseif (!empty($termeni) AND (isset($tip))) {
...
} elseif ((isset($judet)) AND (isset($tip))) {
...
} elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni))) {
...
} else {
// I believe this where it does not get executed.
}
Well, it makes sense why it does not get executed because there is other way that the elseif does not cover. If you look from this point of view
If three variable is set
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
If two variables is set
elseif (!empty($termeni) AND (isset($judet)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
If one variable is set
elseif (isset($tip))
elseif (isset($judet))
elseif (!empty($termeni))
When no variable is set
elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni)))
which leave else condition with nothing to cover.
If I were you, I would structure the code as following.
if (!empty($termeni) and isset($tip) and isset($judet)) {
query = '....';
} elseif (!empty($termeni) and isset($judet) {
query = '....';
} // .... the rest of the condition
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
// write HTML table
} else {
// write message that there is no result found
}
This will reduce the size of your code by 6 times.