Non-noob way to fetch data in Laravel with jQuery - php

I'm rebuilding an application in Laravel and currently use the following code, I guess this would be called AJAX.
$('.date').change(function () {
var date = $('.date').val();
$('#date_summary').html(date);
if (date == "") {
$('#date_summary').html('Choose your service date...');
} else if ($.trim(date) !== '') {
$.get('/calendar.php', {
date: date
}, function (data) {
var data = data.toString();
$('#date_data').html(data);
});
}
});
$('.calendar').trigger('change');
To fetch this code:
if ( isset($_GET['date']) === true && empty($_GET['date']) === false ) {
global $db;
$date = $_GET['date'];
$query = "select * from appointments where Day = :date";
$statement = $db->prepare($query);
$statement->bindValue(':date', $date);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
$eight_to_nine = $result['8AM_9AM'];
$nine_to_ten = $result['9AM_10AM'];
$ten_to_eleven = $result['10AM_11AM'];
$eleven_to_twelve = $result['11AM_12PM'];
$twelve_to_one = $result['12PM_1PM'];
$one_to_two = $result['1PM_2PM'];
$two_to_three = $result['2PM_3PM'];
$three_to_four = $result['3PM_4PM'];
$four_to_five = $result['4PM_5PM'];
$five_to_six = $result['5PM_6PM'];
$six_to_seven = $result['6PM_7PM'];
if ($eight_to_nine != 1) {
echo "<option value='8AM_9AM'>8:00AM - 9:00AM</option>";
}
if ($nine_to_ten != 1) {
echo "<option value='9AM_10AM'>9:00AM - 10:00AM</option>";
}
if ($ten_to_eleven != 1) {
echo "<option value='10AM_11AM'>10:00AM - 11:00AM</option>";
}
if ($eleven_to_twelve != 1) {
echo "<option value='11AM_12PM'>11:00AM - 12:00PM</option>";
}
if ($twelve_to_one != 1) {
echo "<option value='12PM_1PM'>12:00PM - 1:00PM</option>";
}
if ($one_to_two != 1) {
echo "<option value='1PM_2PM'>1:00PM - 2:00PM</option>";
}
if ($two_to_three != 1) {
echo "<option value='2PM_3PM'>2:00PM - 3:00PM</option>";
}
if ($three_to_four != 1) {
echo "<option value='3PM_4PM'>3:00PM - 4:00PM</option>";
}
if ($four_to_five != 1) {
echo "<option value='4PM_5PM'>4:00PM - 5:00PM</option>";
}
if ($five_to_six != 1) {
echo "<option value='5PM_6PM'>5:00PM - 6:00PM</option>";
}
if ($six_to_seven != 1) {
echo "<option value='6PM_7PM'>6:00PM - 7:00PM</option>";
}
}
My question is, is there a more elegant way to do this in Laravel? I'm just trying to implement what are considered best practices. Off the top of my head, I think this would be considered something like an API? Any help is appreciated.

If you are asking about a better way to do this in Laravel, then surely configuring API will be a prominent way to do that.
Simply configure API routes, and in the Controller method use the Laravel Eloquent ORMs to have the Database transactions.
Then finally call the routes in your AJAX request and wait for the response. Then do the final operations based on the returned response from Laravel.

Related

How to check if value of an array is equal to value of another array and display the value as selected in select dropdown

I have two arrays one has list of all majors and other has list of majors of the selected project
, the data is fetched form the database. I want to loop through the list of majors array and if its includes any of the project majors, i want to display it as selected in select drop down.
i have tried using nested while loop for iterating through both array, it but only displays one major instead of two
$majorList = $majorObject->getICTProjectMajor(); // row[0]
$assignedMajor = $projectMajorObject->getProjectMajor(28); //row[0]
if ($assignedMajor != false) {
while ($assignedMajorRow = mysqli_fetch_array($assignedMajor)) {
while ($row = mysqli_fetch_array($majorList)) {
echo "<option value='$row[0]'";
foreach ($assignedMajorRow as $majorID) {
if ($majorID == $row[0]) {
echo " selected";
}
echo ">$row[1]</option>'";
}
echo "</select>";
}
echo "</select>";
}
}
functions
public function getProjectMajor($projectID)
{
if ($this->dbc != NULL) {
$selectQuery = "SELECT Major.majorID FROM ProjectMajor JOIN Major on Major.majorID= ProjectMajor.majorID where projectID=$projectID";
$selectResult = mysqli_query($this->dbc, $selectQuery);
if ($selectResult != FALSE) {
return $selectResult;
} else {
return false;
}
} else {
return false;
}
}
public function getICTProjectMajor()
{
if ($this->dbc != NULL) {
$selectQuery = "SELECT * FROM Major where Major.programmeID=4 ";
$selectResult = mysqli_query($this->dbc, $selectQuery);
if ($selectResult != FALSE) {
return $selectResult;
} else {
return false;
}
} else {
return false;
}
}
The select list currently only shows one major instead of two

Does anyone know how to display one field condition in php

I have a situation where I need to compare 3 different select option fields of the same table. I compared 3 fields, 2 fields and 1 field. The corresponding results are displayed, but in one field condition (category) result are not displayed.
<?php
include'connect.php';
if(isset($_POST['submit']))
{
$qn=$_POST['location'];//LOCATION
$qn1=$_POST['category'];//CATEGORY
$qn2=$_POST['salary'];//SALARY
if(isset($_POST['location']))
{
$q2=mysql_query("SELECT * FROM job_posting where location='$qn'");
while($quew=mysql_fetch_array($q2))
{
echo $ans=$quew['title'];
}
}
else
{
$qir=mysql_query("SELECT * FROM job_posting where category='$qn1'");
while($quew=mysql_fetch_array($qir))
{
echo $gn=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']))
{
$q3=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1'");
while($quew=mysql_fetch_array($q3))
{
echo $ans2=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) && isset($_POST['salary']))
{
$q4=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q4))
{
echo $ans3=$quew['title'];
}
}
if(isset($_POST['location']) || isset($_POST['category']) && isset($_POST['salary']))
{
$q5=mysql_query("SELECT * FROM job_posting where location='$qn' || category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q5))
{
echo $ans4=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) || isset($_POST['salary']))
{
$q7=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' || minsalary='$qn2'");
while($quew=mysql_fetch_array($q7))
{
echo $ans5=$quew['title'];
}
}
}
?>
For a start, I can think of three things
Database Query => check if request the "category"
Database Type => utf8-bin, utf8-general-ci etc.. because is very important how are saved data.
Server file "php.ini" => check "default_charset" and "mbstring..." params, is important to manage string type data.
If you need more help only need send we more information
Try this:
if (!empty($_POST['location'])) {
$lq = "&& location='$_POST['location']'";
}
if (!empty($_POST['category'])) {
$cq = "&& category='$_POST[category]'";
}
if (!empty($_POST['salary'])) {
$sq = "&& minsalary='$_POST[salary]'";
}
$qr = mysql_query("SELECT * FROM job_posting WHERE 1 $lq $cq $sq");
while ($rs = mysql_fetch_array($qr)) {
// do whatever you want
}

PHP verification data error

I have an problem with my PHP verification data.
This my code so far
$cek_saldo=mysql_query
("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
if ($b_id == '$badge_id' AND $mon == '$date_month' AND $b_type == '$jns_saldo')
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('$badge_id', '$saldo', '$bulan', '$jns_saldo', '$date_transaction')
";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
The rule is :
Tabungan Wajib can be submit for 1 time per month. So if twice, it will give error : "Balance for this month has been added before."
Tabungan Tambahan can be submit more than 1 time per month. So if submit more than 1 time, it will saved.
Anyone have a suggestions ?
if ($b_id == "$badge_id" AND $mon == "$date_month" AND $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Your code failed due to the single quotes in the comparison using variables try to learn the difference between single and double quoted strings in php
You don't need to quote.I suppose $badge_id $date_month $jns_saldo come from your form with $_POST
if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
This validation will no work
reason being you are selecting multiple rows from database and assigning only the last on e to $b_id .. $mon etc
and you are using single quotes for comparison
RESOLUTION
select data based on some id if you can
$cek_saldo=mysql_query("SELECT * FROM t_balance where id = 'someid'");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
}
set the error flag
$error = false;
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
{
$error = true;
break
}
}
if($error)
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
Hope it helps
Use this.
$cek_saldo= mysql_query("SELECT * FROM t_balance");
while ($data_cek = mysql_fetch_array($cek_saldo));
{
$b_id = $data_cek['badge_id'];
$mon = $data_cek['month'];
$bal = $data_cek['balance_type'];
if ($b_id == "$badge_id" && $mon == "$date_month" && $b_type == "$jns_saldo")
{
echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
}
else
{
if($_POST)
{
$query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
VALUES ('".$badge_id."', '".$saldo."', '".$bulan."', '".$jns_saldo."', '".$date_transaction."')";
$hasil = mysql_query($query);
if($hasil)
{
echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
}
else
{
echo "<div class='emp'>Gagal menambahkan saldo.</div>";
}
}
}
}

Page Headings and Functions with their headings

I have one more question for the day. I'm trying to make my biography page totally customizable for my custom CMS project I'm doing. If you notice in the view I have 3 h2 tags for Quotes, Allies, Rivals. What I would like to do is put the h3's into my db and then have it do a foreach loop for each one of them so I'm thinking some how I"m going to have to store the function that goes with the pageheading that way it doesn't have to run it if its not active on the page. I know this can be easily done however there's too much for me to focus on what I need to do in order to complete this. Keep in mind that depending on which page you are in the bio will impact which headings will be available.
As of right now here is my controller:
$activeTemplate = $this->sitemodel->getTemplate();
$footerLinks = $this->sitemodel->getFooterNav();
$bodyContent = "bio";//which view file
$bodyType = "main";//type of template
$this->data['activeTemplate'] = $activeTemplate;
$this->data['footerLinks']= $footerLinks;
$this->load->model('biomodel');
if($character !== "jfkdlsjl")
{
if((!empty($character))||(!isset($character))||(trim($character) !== '')||($character !== NULL))
{
$bioArray = $this->biomodel->getCharacterBio($character);
if ($bioArray == "empty")
{
$this->data['bioArray']= array();
}
else
{
if (($bioArray[0]->characters_statuses_id == 2)||($bioArray[0]->characters_statuses_id == 3)||($bioArray[0]->characters_statuses_id == 5))
{
$this->data['bioArray']= array();
}
else
{
$this->data['bioArray']= $bioArray;
$bioPagesArray = $this->biomodel->getBioPages();
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
$this->data['bioPagesArray']= $bioPagesArray;
$this->data['alliesArray']= $alliesArray;
$this->data['rivalsArray']= $rivalsArray;
$this->data['quotesArray']= $quotesArray;
}
}
}
}
And here is my view:
echo "<h2>Quotes</h2>";
if (!empty($quotesArray))
{
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
echo "<h2>Allies</h2>";
if (!empty($alliesArray))
{
echo "<ul>";
foreach ($alliesArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
echo "<h2>Rivals</h2>";
if (!empty($rivalsArray))
{
echo "<ul>";
foreach ($rivalsArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
I don't know what you mean about storing the function nor which function you don't want to run.
Assuming we're working with the last else statement in your controller
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
... and the function you "don't want to run" is the foreach loop on the array in the view, just handle the logic in your view:
if(($this->uri->segment(n)=='pageIwantQuotesOn') && (!empty($quotesArray)){
echo "<h2>Quotes</h2>";
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
...

if else simple beginner issue

Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}

Categories