Laravel Query in if else converting to common query - php

this is my code I want to convert this query in to a common query
if ($limit == 0) {
$response = WebhookErrorNotification::where('is_error', true)->orderByDesc('id')->get();
} else {
$response = WebhookErrorNotification::where('is_error', true)->orderByDesc('id')->limit($limit)->get();
}

You can save the query in a variable above the if/else statement. So something like this would work. Now if this is not the answer you are looking for please specify what you mean by common query!
$query = WebhookErrorNotification::where('is_error', true)->orderByDesc('id');
if ($limit == 0) {
$response = $query->get();
} else {
$response = $query->limit($limit)->get();
}

you can do something like this
$response = WebhookErrorNotification::where('is_error', true)
->when($limit !== 0, function ($query) use ($limit) {
$query->limit($limit);
})->orderByDesc('id')->get();

Related

Combination of where and where is not working

I have a problem code beneath this line does not work! How can I let this work? where ... orWhere orWhere does filter but cumulates the queries. where ... where does not provide any result. Can someone help me?
$artworks = Artwork::where('category_id', $category)
->where('style_id', $style)
->where('technic_id', $technic)
->where('orientation', $orientation)
->get();
Here is the full code:
if (request()->category_id) {
$category = request()->category_id;
} else {
$category = 0;
}
if (request()->style_id) {
$style = request()->style_id;
} else {
$style = 0;
}
if (request()->technic_id) {
$technic = request()->technic_id;
} else {
$technic = 0;
}
if (request()->orientation_id == 'vertical') {
$orientation = 'vertical';
} else if (request()->orientation_id == 'horizontal') {
$orientation = 'horizontal';
} else {
$orientation = 0;
}
$artists = Artist::get();
$artworks = Artwork::where('category_id', $category)
->where('style_id', $style)
->where('technic_id', $technic)
->where('orientation', $orientation)
->get();
return view('frontend.index', compact('artworks', 'artists'));
I think you want to use OR Condition and you are mistaking it with double where. Please look below to understand properly
If you want AND condition in your query then the double where are used but if you want OR condition then you have to use orWhere
Examples:
AND condition
Query::where(condition)->where(condition)->get();
OR Conditon
Query::where(condition)->orWhere(condition)->get();
If you expect all of your variables to be set
Your query variables category_id, style_id, orientation_id & technic_id are being defaulted to 0 if they are not true.
Your query is fine, but you may not have the data you think you do.
Run the following at the top of this function:
print_r($request->all());
exit;
If all of your variables are optional
very procedural, basic way to achieve this:
$artists = Artist::get();
$artworks = Artwork::where('id', '>', 0);
$category_id = request()->input('category_id');
if ($category_id != '') {
$artworks->where('category_id', request()->category_id);
}
$style_id = request()->input('style_id');
if ($style_id != '') {
$artworks->where('style_id', request()->style_id);
}
$technic_id = request()->input('technic_id');
if ($technic_id != '') {
$artworks->where('technic_id', request()->technic_id);
}
$orientation_id = request()->input('orientation_id');
if ($orientation_id != '') {
$artworks->where('orientation_id', request()->orientation_id);
}
$artworks->get();
return view('frontend.index', compact('artworks', 'artists'));

How to check 2 values in 1 table using PHP, jQuery?

Is there a way to check 2 exists values in 1 table ?
It was work just fine when i use only 1 value 'beezName' but when i added another value 'divId' it didn't work, they only check whether the 'beezName' or 'divId' exist in table. I'm trying to get :
if beezName and divId exist then return true else false
How can i make this work? Thanks in advance
Here's my controller : Beez
function checkBeezExists()
{
$beezId = $this->input->post("beezId");
$beezName = $this->input->post("beezName");
$divId = $this->input->post("divId");
if(empty($beezId)){
$result = $this->beez_model->checkBeezExists($beezName, $divId);
} else {
$result = $this->beez_model->checkBeezExists($beezName, $divId, $beezId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
And this is my model : beez_model
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId !=", $beezId);
}
$query = $this->db->get();
return $query->result();
}
You want to check beezName and divId exist but in your model you have wrong condition for beezId
try below code:
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId", $beezId); //<--------remove !=
}
$query = $this->db->get();
return $query->result();
}

GET Multiple MySQL Rows, Form PHP Variables, and Put Into Json Encoded Array

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

Codeigniter active record statement not running when sql query doesn't return anything

I seem to be having a problem with the below code. I cannot get the insert query to run when the query returns nothing. But when there is a row in the db, the else statement runs correctly. Does anybody know why this could be happening?
$query5 = $this->db->get_where('loggedstatus', array('userid_loggedstatus' => $userid));
if ($query5->num_rows() < 0) {
$data1 = array('isLoggedIn_loggedstatus' => 'yes', 'userid_loggedstatus' => $userid);
$this->db->insert('loggedstatus', $data1);
} else {
$data = array('isLoggedIn_loggedstatus' => 'yes');
$this->db->where('userid_loggedstatus', $userid);
$this->db->update('loggedstatus', $data);
}
Have you tried changing this if ($query5->num_rows() < 0) { to something like if ($query5->num_rows() <= 0) {, just a thought. It would make a difference because your telling it to only execute if its less than zero however it might be equal to zero and you would skip to the else statement.
And just for CodeIgniter num_rows() reference:
/**
* Number of rows in the result set
*
* #return int
*/
public function num_rows()
{
if (is_int($this->num_rows))
{
return $this->num_rows;
}
elseif (count($this->result_array) > 0)
{
return $this->num_rows = count($this->result_array);
}
elseif (count($this->result_object) > 0)
{
return $this->num_rows = count($this->result_object);
}
return $this->num_rows = count($this->result_array());
}
Changing if condition shown below should fix the problem.
if ($query5->num_rows() == 0)

php if condition

I can't find anything wrong with this... This should work right?
function ConfirmedNumber()
{
$rs = mysql_query("CALL ConfirmedNumber('" , $_SESSION['UserID'] . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] = 1)
{
return true;
}
}
return false;
}
Assuming the Stored procedure returns a single row with the value '1' in it then I can call the function like this right?
if (ConfirmedNumber())
{
//do some stuff.
}
To expand on my comment:
if ($row['Active'] = 1) should be if ($row['Active'] == 1) to work correctly.
If you want to avoid accidentally doing this in future, you could write your if statements like this:
if (1 == $row['Active'])
This way, you can't accidentally use = as PHP will throw a Fatal Error. You can read more about Comparison Operators at PHP.net
Comment below with the full answer:
The call to the stored proc... line $rs = mysql_query("CALL ConfirmedNumber('" . $_SESSION['UserID'] . "',#Active)"); had a comma instead of the period in the initial post.
you forgot your operator in your IF statement. Change it to this:
if ($row['Active'] == 1)
or even shorter
if ($row['Active'])
it can be something like this
function ConfirmedNumber($id)
{
$rs = mysql_query("CALL ConfirmedNumber('" . $id . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] == 1)
{
return true;
}
}
return false;
}
ConfirmedNumber($_SESSION['UserID']);

Categories