How to select from mysql array text and compare - php

I am collecting user selected "text" as array in mysql using PDO connection from php form there are let suppose 3 check boxes with names
1- Apple
2- Banana
3- Cherry
In DB stored values are as per shown
apple,banana,cherry
What i want : I want to display "Apple" users in certain "/offer.php" page
while i am using SELECT query and it's not working i am using pure php mvc framwork.
In Controller i done this
public function offer(){
$this->view->title = User Selection;
$this->view->offerUser = $this->model->offerUser();
$this->view->render('dashboard/offer');
}
In Model I done this
public function offerUser()
{
return $this->db->select('SELECT * FROM users WHERE selection = apple ORDER BY points DESC ');
}
In View i done this
<?php
foreach($this->offerUser as $key => $value) {?>
<tbody>
<tr>
<td></td>
<td><strong><?php echo $value['selection']?></strong></td>
</tr>
The issue is i am not able to select "Text" from stored array with model and even not able to display selection in "View" user end page.
This code format was working fine with single numeric digit selection or compare but failed with text array.
Please help me out with this i really appreciate this i am new with text array fetching.
Hope someone answer my question soon....
Regards,
james

query is not working : use single quotation around apple
public function offerUser()
{
return $this->db->select("SELECT * FROM users WHERE selection = 'apple' ORDER BY points DESC ");
}

Related

Add row to HTML table with Laravel Livewire - Issue

Firstly I'm new to Laravel and this is my first post. Please forgive me if I'm doing things wrong.
I have an HTML table, which is a Livewire component. In this HTML table, each row has a dropdown box and some text fields. This is for a timesheet application. The dropdown is the job and there are 7 text boxes, for each day of the week for the hours to be entered.
I have tried to include an Add button, I want this to add a new row at the end of the HTML table.
The issue I'm getting is when I click on Add, it will append the row, however, my dropdown is not shown, UNTIL I refresh the page, then it appears as it should.
The way I currently have my program is that when a user clicks on add, it will create a new record in my Entries table, with the timesheet ID included, and the rest of the values as null. Is there another way of temporarily storing the lines the user creates, then commit to the database? Is there a way to refresh my table so that it displays the new line correctly?
Please let me know if you need further details or a better explanation of my issue.
My code is as follows:
Controllers\Livewire\ShowTimesheet.php
...
public function mount($entries)
{
$this->entries = $entries;
}
public function render()
{
return view(‘livewire.show-timesheet');
}
public function add($i)
{
$entries_new = new Entries;
$entries_new->timesheet_id = $i;
$entries_new->save();
$new = array(
'id'=>$entries_new->id,
'timesheet_id'=>$i,
'job_id'=>null,
'worktype_id'=>null,
'hours_sun'=>null,
'hours_mon'=>null,
'hours_tue'=>null,
'hours_wed'=>null,
'hours_thu'=>null,
'hours_fri'=>null,
'hours_sat'=>null,
);
$this->entries[] = $new;
}
...
Layouts\livewire\show-timesheet.blade
...
#foreach($timesheet as $sheet)
...
<div wire:click="add({{$sheet['id']}})" class='cursor-pointer'>
...
#endforeach
...

Yii: Change an input text field value depending on dropdown list selection

I inherited a YII based web application but I am not really familiar with Yii and don't find the solution yet. So if you can, please help me!
There is a form. In the form there is a dropdown list which get data from a db table. This table contains the name and the price of leases. The dropdown lists only the name.
<?php
$typeNames = array();
foreach(LeaseType::model()->findAll() as $lt) {
$typeNames[$lt->id] = $lt->name;
}
?>
<?php echo $form->dropDownListControlGroup($model, 'lease_type_id',
$typeNames,
array(
'empty'=>'Válassz!',
'onchange'=>'$("#model-payed").val($("#model-lease_type_id option:selected").text());',
)
); ?>
But I need to change the value of a specific input text field with the price of the selected lease.
<?php echo $form->textfield($model,'payed');?>
I can get the name of the lease but I can't set the value of the input for the price of the lease. This field has to be changable based on the customer payed all the price or just partial.
Other problem is that the name and price of the lease in different table than the value needed.
I hope all this are understandable because I am not fluent in English.
Thanks for any help!
Finally I found the solution, so share it maybe someone can use it.
In the views/_form.php I modified the dropdownlist:
<?php
echo $form->dropDownListControlGroup($model, 'lease_type_id', $typeNames, array(
'empty' => 'Válassz!',
'onchange' => '$("#model_comment").val($("#model_lease_type_id option:selected").$typeNames);',
//'onchange'=>'if($(this).val() == "no"){("#quantity").val("0"); }'
)
);
?>

Select box returns ID as value not the name

Merry Christmas to you all. Hope we are all having a wonderful time? I'm sure my guess is right. Could someone help me out on this? I don't know why this code returns the id as the value when binding to a select box. All I want to do is to bind a record of MySQL DB to a select box. Though, it is smooth when not using a where clause,
This is the controller:
public function create()
{
$listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'id')->toArray();
return view('product.create')->with('listCompanies', $listCompanies);
}
This is the view:
<p>{!! Form::select('companyname', array('' => 'Select a Company') + $listCompanies) !!} </p>
When I checked the returned source page I found this:
<p><select name="companyname"><option value="" selected="selected">Select a Company</option><option value="1">New Company Nigeria</option><option value="2">Latest Company Nigeria</option></select> </p>
When I select a value from the selectbox, it returns the id, ie, 1, and that is what it adds to the DB not the actual value.
See the attached image for clarification. Please, your input is highly needed.
The option value is what gets saved. If you just want the names, use lists('companyName', 'companyName')
But typically you do want the id in your foreign key field. How do you have the relationship set up?

How to implement search form in codeigniter

I have asked this question before but i did not get the right answer. I have a table 'cv', i want to search for items in the database using two fields i.e. course and location, the two items are on the same table('cv'). I dont have an idea on how to implement the two searches.
The code below is for displaying search for course only and its working fine. Help me to use the two search criterions i.e.course and location or in other words how do i add the search for location input in my code.
Controller
function search_worker()
{
$data['query']=$this->kint_model->search_workers($this->input->post('search'));
$this->load->view('hire_display',$data);
}
Model
function search_workers($search)
{
return $query = $this->db->get_where('cv', array('course '=> $search))->result();
}
View
$data = array('name'=>'search', 'id'=>'search','value'=>'bcom');
echo form_input($data);
$data = array('name'=>'submit', 'id'=>'submit', 'value'=>'Search Item(s)');
echo form_submit($data);
Maybe you need a like clause in your query,
function search_workers($search){
//return $query = $this->db->get_where('cv', array('course '=> $search))->result();
return $this->db->like('course', $search)->like('location', $location)->get('cv')->result();
}
You should also get some basic SQL training.

Best practices for querying an entire row in a database table? (MySQL / CodeIgniter)

Sorry for the novice question!
I have a table called cities in which I have fields called id, name, xpos, ypos. I'm trying to use the data from each row to set a div's position and name.
What I'm wondering is what's the best practice for dynamically querying an unknown amount of rows (I don't know how many cities there might be, I want to pull the information from all of them) and then passing the variables from the model into the view and then setting attributes with it?
Right now I've 'hacked' a solution where I run a different function each time which pulls a value using a query ('SELECT id FROM cities;'), then I store that in a global array variable and pass it into view. I do this for each var so I have arrays called: city_idVar, city_nameVar, city_xposVar, city_yposVar then I know that the city_nameVar[0] matches up with city_xposVar[0] etc. Is there a better way?
I'm not sure what you mean by "set a div's position", but here is an attemp:
controller
$data = array(
'cities' => $this->cities_model->get_cities_info()
);
$this->load->view('view',$data);
model
function get_cities_info()
{
return $this->db->query('SELECT id, name, xpos, ypos FROM cities')->result();
}
view
<?php foreach($cities as $city) : ?>
<div style="position:absolute;top:<?= $city->ypos ?>;left:<?= $city->xpos ?>">
<?= $city->name ?>
</div>
<?php endforearch ?>

Categories