I have a option, where the user can add alert message, and when the page is Loaded, it should be displayed. The problem is that the alert is displayed, but withoud the message, that the user writes and im not sure why.
if( !empty( $postdata ) ) {
foreach( $postdata as $dx_section ) {
var_dump( $dx_section );
foreach ( $dx_section->column_array as $value ) {
if ( $value->column_view == 'alert' && $value->column_indx == $id ) {
$column_data = isset( $value->column_data ) ? $value->column_data : "";
$column_alert = isset( $value->column_alert ) ? $value->column_alert : "";
if ( $column_alert == 'On Page Load' ) {
if (isset( $column_data )) {
echo '<script>alert( "' . $column_data . '" )</script>';
}
} else if ( isset( $value->column_alert_ids ) ) {
$id_array = explode( " ", $value->column_alert_ids );
echo '<script>';
foreach ( $id_array as $key ) {
echo 'jQuery(document).ready(function($){
$(document).find("#' . $key . '").click(function() {
alert("' . $column_data . '");
} );
} );';
}
echo '</script>';
}
}
}
}
}
The column_data is empty, it should be the message.
Here is the dumped $dx_section variable
object(stdClass)[258]
public 'section_order' => int 1
public 'section_name' => string 'First Section Name' (length=18)
public 'section_classes' => string 'Section Classes' (length=15)
public 'section_margin' => string '' (length=0)
public 'section_padding' => string '' (length=0)
public 'column_array' =>
array (size=2)
0 =>
object(stdClass)[257]
public 'row_class' => string '' (length=0)
public 'column_name' => string 'Alert Window 1' (length=14)
public 'column_size' => string '12' (length=2)
public 'col_class' => string 'Column Classes' (length=14)
public 'column_view' => string 'alert' (length=5)
public 'column_indx' => int 1
public 'column_alert' => string 'On Page Load' (length=12)
public 'column_alert_ids' => string '' (length=0)
public 'column_data' => string '' (length=0)
public 'extension_data' =>
object(stdClass)[595]
...
1 =>
object(stdClass)[596]
public 'row_class' => string '' (length=0)
public 'column_name' => string 'Custom Text' (length=11)
public 'column_size' => string '12' (length=2)
public 'col_class' => string 'Column Classes' (length=14)
public 'column_view' => string 'custom_text' (length=11)
public 'column_indx' => int 2
public 'column_class' => string '' (length=0)
public 'column_padding' => string '' (length=0)
public 'column_padding_type' => string '' (length=0)
public 'column_background' => string '' (length=0)
public 'column_boder' => string '' (length=0)
public 'column_data' => string '<p><strong>Custom data...</strong></p>
' (length=39)
public 'extension_data' =>
object(stdClass)[594]
...
Related
I would like to check if the value of a standard class object has a value of 'None'. The object is a PDO and pulls data from the database.
Here is the data from a var_dump:
array (size=1)
0 =>
object(stdClass)[8]
public 'Subid' => string '1' (length=1)
public 'id' => string '27' (length=2)
public 'english' => string 'English Standard' (length=16)
public 'maths' => string 'Mathematics General 2' (length=21)
public 'hsie1' => string 'Modern History' (length=14)
public 'science1' => string 'Biology' (length=7)
public 'science2' => string 'None' (length=4)
public 'science3' => string 'None' (length=4)
public 'tech1' => string 'Agriculture' (length=11)
public 'tech2' => string 'None' (length=4)
public 'extension' => string 'None' (length=4)
public 'arts1' => string 'None' (length=4)
public 'arts2' => string 'Dance' (length=5)
public 'pdhpe1' => string 'None' (length=4)
public 'vet1' => string 'None' (length=4)
public 'start_time' => string '2016-11-15 19:54:08' (length=19)
Is it possible to loop through the keys and test the values?
You can foreach over an array and the public properties of an object
So
foreach ( $array as $obj ) {
foreach ( $obj as $prop => $val ) {
if ( $val == 'None' ) {
echo $prop . ' = ' . $val;
}
}
}
EDIT:
I dont see why you had to change anything here is a quick test I ran
$o = new stdClass;
$o->p1 = 1;
$o->p2 = 'two';
$o->p3 = 'None';
$array = array($o);
var_dump($array);
foreach ( $array as $obj ) {
foreach ( $obj as $prop => $val ) {
if ( $val == 'None' ) {
echo $prop . ' = ' . $val;
}
}
}
And the results were
array(1) {
[0] =>
class stdClass#1 (3) {
public $p1 => int(1)
public $p2 => string(3) "two"
public $p3 => string(4) "None"
}
}
p3 = None
i've to parse an array and print its reslut in a table ,
this is how i am passing array to view in
controller
public function history(){
$history_result= array();
$history_result = $this->user_model->user_history($this->user_id,0);
$this->result_set['data'] = $history_result;
$this->load->view('test', $this->result_set);
}
in view if i var_dump $data i get
array (size=1)
0 =>
object(stdClass)[19]
public 'id' => string '1' (length=1)
public 'user_id' => string '1' (length=1)
public 'sender_name' => string 'test' (length=14)
public 'sender_mobile' => string '12323' (length=10)
public 'receiver_name' => string 'sfsf' (length=4)
public 'sender_location' => string 'sfsf' (length=4)
public 'receiver_location' => string 'sfsfs' (length=5)
public 'receiver_mobile' => string '0' (length=1)
public 'is_urget' => string '0' (length=1)
public 'is_assigned' => string '0' (length=1)
public 'request_type' => string '0' (length=1)
public 'attachment_id' => string '' (length=0)
public 'status' => string '0' (length=1)
my question is how do i access each item out of this result so that may print in a html table like this
<tr>id : 1</tr>
<tr>sender name : name..</tr>
If you have this in your model
return = $this->db->get()->row();//one result
In your view you can display like
$data->id . '<br>' . $data->user_id . '<br' . $data->sender_name ...
if in your model get a multiple rows like
return = $this->db->get()->result();//as many as you have
In your view
foreach($data as $item){
echo '<tr>';
echo '<td>'.$item->id.'</td>';
echo '<td>'.$item->user_id.'</td>';
echo '<td>'.$item->sender_name.'</td>';
...
echo '</tr>';
}
I'm trying to run through an object. I'm having an issue with my initial for each only running through the first iteration. So everything is great, but I'm only getting one iteration while their are 3. Any ideas why this may be happening?
<?php
global $wpdb;
/*Begin*/
$itemsTable = $wpdb->prefix . "HFW_portfolio_items";
$catTable = $wpdb->prefix . "HFW_portfolio_categories";
$tagTable = $wpdb->prefix . "HFW_portfolio_tags";
$rowsItemA = $wpdb->get_results("SELECT * FROM"." $itemsTable"."", ARRAY_A);
exit(var_dump($rowsItemA));
$numItems = count($rowsItem);
$i = 0;
//exit(var_dump($rowsItem));
foreach ($rowsItemA as $rowsItem ){
//$id = $rowsItem[id];
$port_item = "";
$id = stripslashes ($rowsItem[id]);
//exit(var_dump($id));
$portfolio_category = stripslashes ($rowsItem[portfolio_category]);
$sql = "SELECT * FROM"." $catTable"." WHERE id="."$portfolio_category"." LIMIT 1";
$catNameDB = $wpdb->get_results($sql);
/*from Above Select for CATEGORY*/
foreach ($catNameDB as $catNameDBs ){
$portfolio_category = stripslashes ($catNameDBs->cat_name);
}/**/
$portfolio_name = stripslashes ($rowsItem[portfolio_name]);
$portfolio_active = stripslashes ($rowsItem[portfolio_active]);
$portfolio_tags = stripslashes ($rowsItem[portfolio_tags]);
$portfolio_main_image = stripslashes ($rowsItem[main_image]);
$portfolio_desc = stripslashes ($rowsItem[portfolio_desc]);
$image_2 = stripslashes ($rowsItem[image_2]);
$image_3 = stripslashes ($rowsItem[image_3]);
$portfolio_website = stripslashes ($rowsItem[portfolio_website]);
//exit(var_dump($image_2,$image_3));
if($image_2 !== '' || $image_2 = null)
{
$image_2 = ",'".$image_2."',";
}
else
{
$image_2 = '';
}
if($image_3 !== '' || $image_3 = null)
{
$image_3 = ",'".$image_3."'";
}
else
{
$image_3 = '';
}
$port_item .= "
{
'title' : '".$portfolio_name."',
'description' : '".$portfolio_desc."',
'thumbnail' : ['".$portfolio_main_image."' ".$image_2." ".$image_3."],
'large' : ['".$portfolio_main_image."' ".$image_2." ".$image_3."],
'tags' : ['".$portfolio_category."']
}
";
if(++$i === $numItems) {
$port_item .= "";
}
else
$port_item .=",";
//exit(var_dump($i,$numItems));
}
?>
exit(var_dump($rowsItemA));
array (size=3)
0 =>
array (size=10)
'id' => string '9' (length=1)
'portfolio_name' => string 'Da' (length=26)
'main_image' => string 'elicate-dashley-1.png' (length=101)
'image_2' => string 'n-and-mn.png' (length=107)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test1' (length=246)
'portfolio_tags' => string '["1","2","3","4","5","6","10"]' (length=30)
'portfolio_website' => string 'http://www.test.com/' (length=26)
1 =>
array (size=10)
'id' => string '10' (length=2)
'portfolio_name' => string 'Sage' (length=29)
'main_image' => string 'purs-er.png' (length=99)
'image_2' => string '' (length=0)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test 2' (length=249)
'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26)
'portfolio_website' => string 'http://www.test.com/test' (length=27)
2 =>
array (size=10)
'id' => string '11' (length=2)
'portfolio_name' => string 'Scap' (length=20)
'main_image' => string 's-day-cap.png' (length=93)
'image_2' => string '' (length=0)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test 3' (length=155)
'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26)
'portfolio_website' => string 'http://www.test.com/test/test' (length=44)
This is because, you have made $port_item = "" in brginning of the loop.
foreach ($rowsItemA as $rowsItem ){
$port_item = "";// declaring as null here(remove this.)
So in every loop your value is reinitialized, and you are getting only 1 element(last element of your array)
I have variable $related witch is stdCalss object and i want to convert it with one foreach loop in to reffrence array.
Var_dump of Object $related:
array (size=19)
0 =>
object(stdClass)[20]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1567' (length=4)
1 =>
object(stdClass)[21]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1562' (length=4)
2 =>
object(stdClass)[22]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1564' (length=4)
3 =>
object(stdClass)[23]
public 'id_product' => string '1568' (length=4)
public 'related' => string '1410' (length=4)
4 =>
object(stdClass)[24]
public 'id_product' => string '111' (length=3)
public 'related' => string '77' (length=2)
5 =>
object(stdClass)[25]
public 'id_product' => string '111' (length=3)
public 'related' => string '1610' (length=4)
Php code:
foreach ($related AS $r){
????
}
var_dump($rels);
Wished Output:
$rels = array(
'1568'=>'1567, 1562, 1564, 1410',
'111'=>'77,1610'
);
Build a temporary array and implode it:
foreach($related AS $r) {
$temp[$r->id_product][] = $r->related;
$rels[$r->id_product] = implode(', ', $temp[$r->id_product]);
}
print_r($rels);
$rels = array ();
foreach ($related as $r){
$rels[$r->id_product] .= $r->related . ","; // put all of them with respective key together
}
$rels = array_map(
function ($a) {
$a = preg_replace('~,(?!.*,)~', '', $a);
return $a;
}
,$rels); // remove last commas
var_dump($rels);
Try,
$rels = array();
foreach($related as $r){
$rels[$r->id_product] .= $r->related.', ';
}
array_walk_recursive($related, function(&$item){
$item = rtrim($item,', ');
});
From the PHP docs: http://php.net/manual/en/language.types.type-juggling.php
How do I display the following using the array shown below?
Pastors
key=>0, member_id, member_name
Deacons
key=>1, member_id, member_name
key=>2, member_id, member_name
Here is the array.
array (size=3)
0 =>
array (size=4)
'category_name' => string 'Pastors' (length=7)
'member_id' => string '3' (length=1)
'member_name' => string 'Tiny Marshall' (length=13)
'member_email' => string 'jconley#nowhere.com' (length=19)
1 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '1' (length=1)
'member_name' => string 'Jeremiah Conley' (length=15)
'member_email' => string 'jconley#nowhere.com' (length=19)
2 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '2' (length=1)
'member_name' => string 'Marcy Conley' (length=12)
'member_email' => string 'jconley#nowhere.com' (length=19)
Here is the code that I used to build the array:
while( $row = mysql_fetch_assoc( $result ) )
{
$staff[$i] = array
(
'category_name' => $row['category_name'],
'member_id' => $row['member_id'],
'member_name' => $row['member_name'],
'member_email' => $row['member_email'],
);
$i++;
}
This is the final solution:
$category_name = array();
foreach ($staff as $member) {
if (!in_array( $member['category_name'], $category_name ))
{
echo '<h1>' . $member['category_name'] . '</h1>';
$category_name[] = $member['category_name'];
}
echo $member['member_id'] . ', ' . $member['member_name'] . '<br />';
}
Use foreach to loop over your $staff array.
$pastors = array();
$deacons = array();
foreach ($staff as $member) {
if ($member['category_name'] == 'Pastors') {
$pastors[] = $member['member_id'] . ', ' . $member['member_name'];
} else if ($member['category_name'] == 'Deacons') {
$deacons[] = $member['member_id'] . ', ' . $member['member_name'];
}
}
documentation