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)
Related
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]
...
I want to make a multiple where query with AND and OR. Suppose my array is:
array (size=8)
0 => array (size=2)
'timeline.type' => string 'videos' (length=6)
'timeline.sourceId' => string '7' (length=1)
1 => array (size=2)
'timeline.type' => string 'loadshedding' (length=12)
'timeline.sourceId' => string '5' (length=1)
2 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '3' (length=1)
3 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '5' (length=1)
The above array could be dynamic. I want to make active record query like:
(timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.
I tried :
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
foreach($sources as $source){ //$sources is an array like given above
$this->db->or_where($source);
}
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);
But when I echo $this->db->last_query(); to see the query. It returns SELECT * FROM timeline only. What might be the problem. Thank you.
Your Code Should be like
$sources = array (
0 => array (
'timeline.type' => 'videos',
'timeline.sourceId' => '7' ),
1 => array (
'timeline.type' => 'loadshedding',
'timeline.sourceId' => '5' ) ,
2 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '3' ),
3 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '5')
);
end($sources); // move the internal pointer to the end of the array
$lastkey = key($sources);
$pr = "";
foreach($sources as $key=>$source)
{
if($key != $lastkey)
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ") OR ";
}
else
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ")";
}
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);
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
How can I force all numeric values to be integer instead of string when some PHP function takes place for exemple with array_replace() ? here is an example:
My $item is an array of default values, which var_dump($item) produces this:
array (size=12)
'id' => string '' (length=0)
'cid' => int 2
'pid' => string '' (length=0)
'rid' => string '' (length=0)
'section' => int 0
'title' => string '' (length=0)
'slug' => string '' (length=0)
'image' => string '' (length=0)
'description' => string '' (length=0)
'ordering' => string '' (length=0)
'created' => string '' (length=0)
'modified' => string '' (length=0)
Then, i call a function to update $item array with new values which comes from db with a function array_replace($item, $item_db);, and when I var_dump($item) again, i get this:
array (size=12)
'id' => string '12' (length=2)
'cid' => string '1' (length=1)
'pid' => string '0' (length=1)
'rid' => string '37' (length=2)
'section' => string '0' (length=1)
'title' => string 'Article2' (length=8)
'slug' => string 'articles123' (length=11)
'image' => string 'e9213e52d235bd892b3337fce3172bed.jpg' (length=36)
'description' => string '' (length=0)
'ordering' => string '3' (length=1)
'created' => string '2014-05-15 14:51:10' (length=19)
'modified' => string '2014-05-15 23:29:40' (length=19)
I want to be all numeric values (id, cid, pid, rid, section, ordering) the integer, except created and modified keys.
How I suppose to do that without manually write each time something like:
$item['section'] = (int) $item['section'];
Is there any solution for this ?
You can use such simple foreach loop:
foreach ($array as $k => $v) {
if ($k != 'created' && $k != 'modified') {
$array[$k] = (int) $v;
}
}
This is of course if you are sure that all values are numeric so they can be converted to int. Otherwise you have to use:
foreach ($item as $k => $v) {
if (is_numeric($v)) {
$item[$k] = (int) $v;
}
}
Create an array containing the values which you'd like to not be renamed. Then loop through your array — on each iteration, check if the current key is in the $defaults array. If it is not, push it into a new array ($results) with the current numeric offset as the key. If not, push it into the new array with the current key:
Something along the lines of:
$defaults = ['created', 'modified']; // Keys to be left untouched
$result = []; // Results array
$i = 0; // Numeric offset
foreach ($array as $key => $value) {
if (!in_array($key, $defaults)) {
$result[++$i] = $value;
} else {
$result[$key] = $value;
}
}
print_r($result);
In my last question, was created a way to divide a array based in the string pattern Father_son, but now i need a way to split too a new pattern: Father_N_son
Original (generated by http://ideone.com/pmHHR code) :
array
'Pagamento' =>
array
'data' => string '' (length=0)
'0_pessoaId' => string '85' (length=2)
'0_valorBruto' => string '890.00' (length=6)
'1_pessoaId' => string '83' (length=2)
'1_valorBruto' => string '20.00' (length=5)
Now i need:
array
'Pagamento' =>
array
'data' => string '' (length=0)
0 =>
array
'pessoaId' => string '85' (length=2)
'valorBruto' => string '890.00' (length=6)
1 =>
array
'pessoaId' => string '83' (length=2)
'valorBruto' => string '20.00' (length=5)
Thanks,
Celso
my solution with regex:
static function parserRequest($array = null) {
if (empty($array)) {
$array = $_REQUEST;
}
foreach ($array as $k => $v) {
$name = explode('_', $k);
$newkey = array_shift($name);
$newname = implode('_', $name);
//para o padrão Entidade.n.atributo
if(preg_match("/[0-9]_[a-z]/", strtolower($newname))){
$nameValued = explode('_',$newname);
$newkeyValued = array_shift($name);
$newnameValue = implode('_', $name);
$result[$newkey][$newkeyValued][$newnameValue] = $v;
}else{
//para o padrão Entidade.atributo
$result[$newkey][$newname] = $v;
}
}
return $result;
}