CodeIgniter Multiple Row Update - php

I'm having a problem with a project of mine. I have read around and have no idea on what to do any more. This is a Codeigniter todo list project for our small office. I have made the displaying the contents of the database part already however I'm stumped on the updating part. On the view part, there's a todo list with checkboxes. If they are completed with the task, then they check the box and then put in the date when they did it and their initials. So they can check a lot of tasks and put dates and initials and I'm having a problem updating them in one time. I'm stumped. Please help. Thanks!
Controller
public function update_tasks() {
$this->load->model('model_members');
$post=$this->input->post();
$this->model_memberdashboard->reset_tasks($this->session->userdata('UserID')); // resets completion status of all tasks since checkboxes that are not checked doesn't do Post
foreach ($post['checkbox'] as $row){
$newData = array(
'completed'=> 1
);
$this->model_members->update_tasks($newData,$row);
}
$this->index();
Model
public function update_tasks($data,$id) {
$this->db->update("mastertaskslist",$data,['id' => $id]);
}
View <- Im using bootstrap so its a bit long
foreach ($allowedTasklist_1 as $row) {
echo '<li class="ui-state-default clearfix">';
echo '<div class=row>';
echo '<div class="col-sm-1">';
$checkbox=array(
'name' => 'checkbox[]',
'id' => 'checkbox[]',
'type' => 'checkbox',
'value' => $row->id,
'checked' => $row->completed,
);
if ($row->completed=="False"){
unset($checkbox['checked']);
}
echo form_checkbox($checkbox);
echo '</div>';
echo '<div class="col-sm-1">';
echo '<label>'.$num.'.'.'</label>';
echo '</div>';
echo '<div class="col-sm-10">';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo '<p>'.$row->taskTitle.'</p>';
echo '</div>';
echo '</div>';
echo '<div class="row">';
echo '<div class="col-sm-8">';
if ($row->lastUpdate=='0000-00-00'){
$currDate=NULL;
} else {
$currDate=date('m/d/Y', strtotime($row->lastUpdate));
}
$date=array(
'type' => 'text',
'class' => 'datepicker',
'name' => 'date['.$row->id.']',
'id' => 'date['.$row->id.']',
'value' => $currDate,
'placeholder' => 'mm/dd/yyyy',
'size' => '15'
);
echo form_input($date);
echo '</div>';
echo '<div class="col-sm-4">';
$initials=array(
'name' => 'initials['.$row->id.']',
'id' => 'initials[]',
'value' => $row->initials,
'maxlength' => '100',
'size' => '3',
);
echo form_input($initials);
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
$num++;
echo '</li>';
}

Related

How to create a picture gallery in an extensive php file?

I am new to php and want to create a simple gallery using an extensive file and implementing it in the index.php: I create an array and use a foreach loop to add pictures with some additional info. Something is obviously wrong with the logic or/and the syntax : ( All I get so far is a blank red rectangle.
<?php>
$landscape = array(
'pic1'=> array('name'=>'picture1.jpg','label'=>'p1', 'text'=>''),
'pic2'=> array('name'=>'picture2.jpg','label'=>'p2', 'text'=>''),
'pic3'=> array('name'=>'picture3.jpg','label'=>'p3', 'text'=>''),
'pic4'=> array('name'=>'picture4.jpg','label'=>'p4', 'text'=>''),
);
$galleriya='';
foreach ($landscape as $key => $value) {
$galleriya .= "<div>
<div style='float:left;height:140px;width:200px;border:1px solid red;'>
<img src=$value['name'] alt=''>
</div>"
."<div style='font-size:50px;'>$value['label']</div>"
."<div style='font-size:50px;'>$value['text']</div>
</div>"
};
echo($galleriya);
?>
I've tidied up the syntax stuff within the foreach loop. You need to have a read of this article, which has a brilliant explanation of when to use single and double quotes.
What is the difference between single-quoted and double-quoted strings in PHP?
$landscape = array(
'pic1' => array('name' => 'picture1.jpg', 'label' => 'p1', 'text' => ''),
'pic2' => array('name' => 'picture2.jpg', 'label' => 'p2', 'text' => ''),
'pic3' => array('name' => 'picture3.jpg', 'label' => 'p3', 'text' => ''),
'pic4' => array('name' => 'picture4.jpg', 'label' => 'p4', 'text' => ''),
);
$galleriya = '';
foreach ($landscape as $key => $value) {
'<div style="float:left">'.
'<div style="height:140px;width:200px;border:1px solid red;">
<img src="' . $value["name"] . '">
</div>'
. '<div style="font-size:50px;">' . $value["label"] . '</div>'
. '<div style="font-size:50px;">' . $value["text"] . '</div>
</div>';
}
echo($galleriya);
For the images to display, they will need to be in the same folder as this script. If you wanted to move them to another folder such as images, then the images folder must be in the same place as this script and the source would be modified to (for example):
<img src="images/' . $value["name"] . '"

How to display associate arrays in a table php

Right i will start by showing you the code as it will make sense quickly
private static $cases = [
'100' => [
'id' => '1',
'case_no' => '1',
'name' => 'First Case'
],
'101' => [
'id' => '2',
'case_no' => '2',
'name' => 'Second Case'
],
];
Now i want a table with 3 columns and two rows.
The column headers should be id case_no and name.
foreach($results as $key => $value){
echo '<tr>';
echo "<td>";
echo
echo "</td>";
echo "<td>";
echo
echo "</td>";
echo "<td>";
echo
echo "</td>";
echo '</tr>';
}
what do i need to put in there to make this happen?
EDIT::
In my model i find a specific case by the $case_no someone searches for.
public static function findByCaseNo($case_no)
{
foreach (self::$cases as $case) {
if (strcasecmp($case['case_no'], $case_no) === 0) {
return new static($case);
}
}
return null;
}
Then my Controller send back to the view results as that specific case
return $this->render('search', [
'model' => $model,
'dataProvider' => $model->findAll(),
'results' => $case,
'case_no' => $case_no
]);
You either need two loops or call it key by key
echo "<table>";
foreach($cases as $key=> $value){
echo "<tr>";
echo "<td>".$value['id']."</td>";
echo "<td>".$value['case_no']."</td>";
echo "<td>".$value['name']."</td>";
echo "</tr>";
}
echo "</table>";

How to call a function for a select box?

Hi I have this function on PHP:
<?php
class ConectorDatos {
static function buscarProductos() {
return array(
'Hom' => array( '1VX' => 649.95 ),
'Sam' => array( 'Note2' => 699.95,
'Gala' => 499.95,
'Gel' => 249.95),
'olivi' => array( 'Lumia' => 999.95),
'Obvow' => array( 'One Plus One' => 299.50 )
);
}
And the HTML Page with this code section:
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:</li>
<select name="marc" id="marc4">
<?php
****************
?>
</select>
<li>Modelo:</li>
<li>Precio:</li>
So my question is simple but I don't know how to do it...how HTML on the "select" section could "call" the phpfunction and show the necessary information on webpage. What do I have to do?
You need to include 1st php file in 2nd one like this
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:</li>
<select name="marc" id="marc4">
<?php
require("file_name");
$obj = new ConectorDatos();
$data=$obj-> buscarProductos();
foreach($data as $key=>$value)
echo "<option>$key</option>";
?>
</select>
<li>Modelo:</li>
<li>Precio:</li>
<?php
class ConectorDatos {
public static function buscarProductos() {
return array(
'Hom' => array( '1VX' => 649.95),
'Sam' => array( 'Note2' => 699.95,
'Gala' => 499.95,
'Gel' => 249.95),
'olivi' => array( 'Lumia' => 999.95),
'Obvow' => array( 'One Plus One' => 299.50)
);
}
}
?>
Click here for a DEMO
<?php $producto = ConectorDatos::buscarProductos();?>
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:<?php echo $producto['Hom']['1VX'];?></li>
<select name="marc" id="marc4">
<?php
echo "<option value='".$producto['Sam']['Note2']."'>".$producto['Sam']['Note2'] . "</option>";
echo "<option value='".$producto['Sam']['Gala']."'>".$producto['Sam']['Gala'] . "</option>";
echo "<option value='".$producto['Sam']['Gel']."'>".$producto['Sam']['Gel'] . "</option>";
?>
</select>
<li>Modelo:<?php echo $producto['olivi']['Lumia'];?></li>
<li>Precio:<?php echo $producto['Obvow']['One Plus One'];?></li>

How To Price included in the Osclass RSS Feed Title?

I would like to ask on how to include the price in the RSS Feed Title in website running with Osclass.
Like This [Price / Title (Contact Number)]
public function dumpXML() {
echo '<?xml version="1.0" encoding="UTF-8"?>', PHP_EOL;
echo '<rss version="2.0">', PHP_EOL;
echo '<channel>', PHP_EOL;
echo '<title>', $this->title, '</title>', PHP_EOL;
echo '<link>', $this->link, '</link>', PHP_EOL;
echo '<description>', $this->description, '</description>', PHP_EOL;
foreach ($this->items as $item) {
echo '<item>', PHP_EOL;
echo '<title><![CDATA[', $item['title'], ']]></title>', PHP_EOL;
echo '<link>', $item['link'], '</link>', PHP_EOL;
echo '<guid>', $item['link'], '</guid>', PHP_EOL;
echo '<description><![CDATA[';
echo $item['description'], ']]>';
echo '</description>', PHP_EOL;
echo '<country>', $item['country'], '</country>', PHP_EOL;
echo '<region>', $item['region'], '</region>', PHP_EOL;
echo '<city>', $item['city'], '</city>', PHP_EOL;
echo '<cityArea>', $item['city_area'], '</cityArea>', PHP_EOL;
echo '<category>', $item['category'], '</category>', PHP_EOL;
echo '</item>', PHP_EOL;
}
echo '</channel>', PHP_EOL;
echo '</rss>', PHP_EOL;
}
}
Thanks You
Normally, I would tell you not to modify the core. You have a hook available designed for this purpose : 'feed' but it seems you can't access the data. So you'll have to modify the core.
Add a line 'price' => osc_item_formated_price() to both addItem() calls in oc-includes/controllers/search.php :
while(osc_has_items()) {
if(osc_count_item_resources() > 0){
osc_has_item_resources();
$feed->addItem(array(
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url(), ENT_COMPAT, "UTF-8" ),
'description' => osc_item_description(),
'country' => osc_item_country(),
'region' => osc_item_region(),
'city' => osc_item_city(),
'city_area' => osc_item_city_area(),
'category' => osc_item_category(),
'dt_pub_date' => osc_item_pub_date(),
'image' => array( 'url' => htmlentities(osc_resource_thumbnail_url(), ENT_COMPAT, "UTF-8"),
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8") )
));
} else {
$feed->addItem(array(
'title' => osc_item_title(),
'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8"),
'description' => osc_item_description(),
'country' => osc_item_country(),
'region' => osc_item_region(),
'city' => osc_item_city(),
'city_area' => osc_item_city_area(),
'category' => osc_item_category(),
'dt_pub_date' => osc_item_pub_date()
));
}
}
Then you'll be able to modify the RSSfeed::dumpXML method by adding a echo $item['price'] somewhere.
PS:
I'll try to make a commit in the Osclass Github to make the feed hook usable.

How to echo checkbox group with multidimentional array

I need help to get the data from my checkbox-group with multidimensional array options to reflect in my post page(single.php code). Radio type is working well but the checkbox-group type is not. I added on the bottom the sample code found in my single.php for the radio type which query the data to my post page for your reference.
Here's the array from my metabox.php code:
<?php
// array
$prefix = 'wtf_';
$meta_box = array( 'id' => 'site',
'title' => 'Program Details',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array('name' => 'Principal Return',
'desc' => 'Principal Return After Expiry or Not',
'id' => $prefix . 'principal',
'type' => 'radio',
'options' => array(
array('name' => ' Yes ', 'value' => 'Yes-after expiry'),
array('name' => ' No ', 'value' => 'No-included on the interest')
)
),
array(
'name' => 'Compounding',
'desc' => 'Choose if compounding is allowed or not',
'id' => $prefix . 'compounding',
'type' => 'radio',
'options' => array(
array('name' => ' Yes ', 'value' => 'Allowed'),
array('name' => ' No ', 'value' => 'Not Allowed'),
array('name' => ' Re-purchase', 'value' => 'Yes thru re-purchase')
)
),
array ('name' => 'Payment Processors',
'desc' => 'Payment Processsor Accepted',
'id' => $prefix.'processors',
'type' => 'checkbox_group',
'options' => array(
array('label' => ' Liberty Reserve ', 'value' =>'LR'),
array('label' => ' SolidTrustPay ', 'value' =>'STP'),
array('label' => ' EgoPay ', 'value' =>'EgoPay'),
array('label' => ' Perfect Money ', 'value' =>'PM'),
array('label' => ' Payza ', 'value' =>'Payza'),
array('label' => ' PayPal ', 'value' =>'PayPal'),
array('label' => ' Bankwire ', 'value' =>'Bankwire')
))))
// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo $statetemt;
break;
case 'textarea':
echo $statetemt;
break;
case 'select':
echo $statetemt;
break;
case 'radio':
foreach ($field['options'] as $option) {
echo $statetemt; }
break;
case 'checkbox':
foreach ($field['options'] as $option) {
echo $statetemt;}
break;
case 'checkbox_group':
foreach ($field['options'] as $option) {
echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />',$option['label']; }
echo '<br /><span class="description">'.$field['desc'].'</span>';
break;
}
//From my single.php code <<<<=================
<div class="sdinfo"><strong>Principal Return</strong>:<span><?php $principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
if (isset($principal[0])) {
echo $principal ;
} else if (isset($principal[1])) {
$principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
echo $principal;
} else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Program Started</strong>:<span> <?php $started = get_post_meta(get_the_ID(), 'wtf_started', true); if (isset($started[0])) { echo $started;
} else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Compounding</strong>:<span>
<?php $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
if (isset($compounding[0])) {
echo $compounding ;
} else if (isset($compounding[1])) {
$compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
echo $compounding;
} else if (isset($compounding[2])) {
$compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
echo $compounding;
} else {_e('Not Available');} ?></span></div>
?>
This give me an output from post meta like this:
admin screenshot
This is the output from my post page. :
post page screenshot
Please help!.. I am not a programmer hope you can share me an answer in much details.Thank you in advance!
All what you need is point an array using var_dump($array) to check where and what data you need from arrays fields.
Second you need to get foreach() function. For example you want grab an array of data:
'priority' => 'high', should be as example:
echo $meta_box["priority"] which will result as value of array: high
If you are not sure and not familiar with array use as simple:
foreach ($meta_box as $arr)
{
var_dump($arr);
#then play and manipulate how to grab a fields a data:
foreach ($arr["fields"][""] as $fa)
{
var_dump($fa["name"]);
var_dump($fa["desc"]);
var_dump($fa["desc"]);
}
#...
}
Learn how to grab via var_dump() if you unsure with array of data.
Payment Processors LR STP EgoP
echo $meta_box["fields"][""]["name"];
Than you need grab a data of an array:
foreach ($meta_box["fields"][""]["options"] as $options)
{
foreach ($options as $options_key)
{
foreach ($options_key as $opt_val)
{
$result .= ' '.$opt_val;
}
}
}

Categories