How to get selected value in dropdownlist by using cakephp? - cakephp

My code is something like below :
public function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
And here is the drop down list code :
$this->Form->select('ClubOpenDay.0.open_time', $this->makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
My problem is I am getting the value like 11 am, 12 am.But I want to make it selected when I will get value 11 am or 12 am from database.Any idea how can I do this?

Please try this:
<?php echo $this->Form->select('ClubOpenDay.0.open_time',$this->makeEntryTime(),array("empty" => false,'value' => $value_selected,'class' => 'input-medium')); ?>
"$value_selected" is any value with in the input range.
i.e what $this->makeEntryTime() returns.
Thanks

I assume your method is in view which you shouldn't do this. remove public from your function and remove $this to call your function.
try to keep your method inside a controller at least. Here is if you want to keep your method in view.
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
echo $this->Form->select('selete_id', makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
but if you want to make it MVC.
//controller
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
//page function - for examlpe add() method
public function add(){
$this->set('times', $this->makeEntryTime());
}
//view of add method
echo $this->Form->select('time_id', $times, array("empty" => false, 'class' => 'input-medium'));

Related

Laravel - Save multiple field values in json/array in a column

I have 3 form fields name, address, profile_photo. I want to save there 3 fields in json/array format in a single column so that I can retrieve it later in view blade.
My form looks like this
I tried
$customer_details= new Customer;
{
$profile_photo = $request->file('profile_photo');
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$cust_details=json_encode(request(['name', 'address', $profile_ph]));
$customer_details->details = $cust_details;
$customer_details->save();
}
With this profile_photo is not saved but other data are saved as:
{"name":["john","Sam"],"address":["CA","FL"]}
How can I save all there fields including profile_photo and show each details distinctly later on view blade?
Hope this will help you.
$customer_details = new Customer;
$profile_photo = $request->file('profile_photo');
$profile_ph = [];
for ($i = 0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph[$i] = '/images/' . $pro_fileNameToStore;
}
$data = [
'name' => $request->name,
'address' => $request->address,
'profile_ph' => $profile_ph
];
$customer_details->details = json_encode($data);
$customer_details->save();
The details column should be text if you are using mysql.
Try this:
$names = $request->input('name');
$addresses = $request->input('address');
$profile_photos = $request->file('profile_photo');
for ($i = 0; $i < count($names); $i++) {
$customer_details = new Customer;
$pro_fileExt = $profile_photos[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photos[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph = '/images/' . $pro_fileNameToStore;
$cust_details = json_encode([
'name' => $names[$i],
'address' => $addresses[$i],
'profile_photo' => $profile_ph,
]);
$customer_details->details = $cust_details;
$customer_details->save();
}
Try this with single database query
$profile_photo = $request->file('profile_photo');
$finalData = [];
$names = [];
$addresses = [];
$images = [];
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$names[] = $request->input('name');
$addresses[] = $request->input('address');
$images[] = $profile_ph;
}
$cust_details=json_encode(['name' => $names, 'address' => $addresses, 'images' => $images]);
$customer_details->insert($cust_details);

Combined two arrays and the result returned has an empty key and value

I have an excel sheet which is read and converted to an array.
Here is the code :
$filename = $_FILES['importProjects']['tmp_name'];
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$i = 0;
$all_sheet = $objPHPExcel->getSheetCount(); //total number of sheets
$arrayInsertProject = array();
$arrayUpdateProject = array();
while ($i < $all_sheet){
$objPHPExcel->setActiveSheetIndex($i);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of rows from the sheet
$column = 'A';
$project_cnt=1;
$dataIns = array();
for ($row = 1; $row <= $arrayCount; $row++) { //loop through rows
if($row ==1){
$highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
$rowDataHeader = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$rowDataHeader = array_reduce($rowDataHeader, 'array_merge', array());
}else {
$highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
$rowData = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$dataIns = array_combine($rowDataHeader, $rowData);
}
}
}
When I print this array $dataIns, I get something like this :
Array
(
[intProjectSlNo] => 1069
[chvProjectNameEng] => New
[slug] => this-is -test
[nchvSecType] => 1754349
[] =>
)
How to remove the empty key => value pair ?
Add this after while loop of your code
foreach($dataIns as $key=>$value)
{
if(is_null($value) || $value == '')
unset($dataIns[$key]);
}
print_r($dataIns);

Find product in database with "like" statement in cakephp

I have a function that searches in the database all products with the description like the user search, but it is not working. Where is the problem?
public function searchProduct(){
$search = $this->request->query('search');
$products = $this->Product->find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
if($this->Auth->user()!= null){
$userId = $this->Auth->user('id');
$user = $this->User->findById($userId);
$filters = $user['Filter'];
$arrayFiltersOfUser = array();
$arrayOfProductIds = array();
for($i = 0; $i < count($filters); $i++){
$arrayFiltersOfUser[$i] = $filters[$i]['id'];
}
for($i = 0; $i < count($products); $i++){
$arrayOfProductIds[$i] = $products[$i]['Product']['id'];
}
for($i = 0; $i < count($arrayOfProductIds); $i++){
$arrayFiltersOfProduct_aux[$i] = $this->FiltersProduct->findAllByProductId($arrayOfProductIds[$i]);
$array = $arrayFiltersOfProduct_aux[$i];
for($j = 0; $j < count($array); $j++){
$array2[$j] = $array[$j]['FiltersProduct']['filter_id'];
}
$containsAllValues = !array_diff($arrayFiltersOfUser, $array2);
if($containsAllValues == true){
$this->set("is_valid", "yes");
$this->set("_serialize", array("is_valid"));
}
else{
$this->set("is_valid", "no");
$this->set("_serialize", array("is_valid"));
}
}
}
$this->set("response", $products);
$this->set("_serialize", array("response"));
}
You're now searching for: %$search% and not the value of $search.
Try to replace
find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
with
find("all",array('conditions'=>array('Description LIKE'=>"%".$search."%")));
This should do the trick.

Undefined offset: 1 twitch channel grab

I get this on my website:
Notice: Undefined offset: 1 in ...
Here is the full code, bolded is the part it refers to i think, basically this scripts should grab list of my channels from ther DB and then display info from the twitch:
<?php
defined('_JEXEC') or die('Direct access to this location is not allowed.');
$userList = $params->get('userlist');
$usersArray = explode(',', $userList);
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($usersArray as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
//used to be $viewer = $json_array[$i]['stream_count'];
**foreach($usersArray as $i =>$value){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$name = $json_array[$i]['name'];
$game = $json_array[$i]['meta_game'];
$viewer = $json_array[$i]['channel_count'];
$topic = $json_array[$i]['title'];
onlinecheck($member, $viewer, $topic, $game);
$checkedOnline[] = signin($member);
}**
unset($value);
unset($i);
function onlinecheck($online, $viewers, $topic, $game)
{
if ($game == "Counter-Strike: Global Offensive")
{
$igra = "csgo12.jpg";
}
else{
$igra = "online.png";
}
if ($online != null)
{
echo ' <img src="./modules/mod_twitchlist/tmpl/'.$igra.'">';
echo ' <strong>'.$online.'</strong>';
echo ' (' .$viewers.') - ';
echo '<strong>'.$topic.'</strong> </br>';
}
}
function signin($person){
if($person != null){
return $person;
}
else{
return null;
}
}
?>
<!-- <hr> -->
<?php
foreach ($usersArray as $i => $value1) {
foreach($checkedOnline as $ii => $value2){
if($value1 == $value2){
unset($usersArray[$i]);
}
}
}
$broj1 = count($usersArray); //neaktivni korisnici
$broj2 = count($checkedOnline); //ukupno streamova
if ($broj1 == $broj2){
echo '<strong><center>Nijedan stream nije aktivan trenutno!</center></strong>';
}
?>
Any hints?

Why I can't get values from this web service Array?

Can't figure out why this web service don't work. Just gives me blank. I tested the url and the data it's all there.
http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0
my code:
session_start();
function getNews() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0');
$data = json_decode($json, TRUE);
$newst = array();
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
foreach($newst as $v)
{
$_SESSION['newsid'][] = $v['id'];
$_SESSION['newstitle'][] = $v['title'];
$_SESSION['newstext'][] = $v['news'];
$_SESSION['newslink'][] = $v['link'];
$_SESSION['newsdate'][] = $v['date'];
$_SESSION['newsentityName'][] = $v['entityName'];
$_SESSION['aclikes'][] = $v['account']['likes'] . ")";
$_SESSION['acdislikes'][] = $v['account']['dislikes'] . ")";
$_SESSION['accomentes'][] = $v['account']['commentes'] . ")";
$_SESSION['acshares'][] = $v['account']['shares'] . ")";
$_SESSION['acclicks'][] = $v['account']['clicks'] . ")";
}
}
getNews();
$key = count($_SESSION['newsid']);
for ($i = 0; $i <= $key; $i++) {
echo $_SESSION['newsid'][$i] . "<br />";
}
Are you sure that this line is correct:
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
Reading this makes me think that you are overwriting the entire $newst array with a single item... for each item. Thus the array will end up being the value of the last $item (which might be empty).
I'd expect something like:
foreach($data['data']['item'] as $item) {
$newst.push( $item );
}
(note, not tested and my syntax may be dodgy... but you get the drift).

Resources