Jump to content

How could this possibly be?? (A simple and concise question)


Inigo

Recommended Posts

This is a function that is being called from an ajax request. I can arrange things so that I get the 'no images' msg, but NOT the 'yep, there\'s images'.

 

function build_post($post){

if ($post['image_data'] == 0){

return 'no images.';

} else {

return 'yep, there\'s images';

}

}

 

If I change the function to just return $post['image_data'], I get an 'Object Object in my javascript alert, meaning the php process is working correctly so there can't be any errors in constructing the array up until this point. (I can also access each of the possible array elements by changing it to return $post['image_data'][1]['alignment'] or whatever.. so like I say, I'm sure there are no errors in the array construction)

 

I really can't for the life of me then, figure out why I can't get the 'yep there's images' msg. I can probably work around this, but it's bugging me, so if anyone could throw a few possibilities as to how this could be, then I'd be most grateful. Thanks

Link to comment
Share on other sites

yeah no, doesn't work I'm afraid. It's a bit more complicated as the variable name is changing as I'm passing it between functions.. I'm using codeigniter my full code is as follows:

 

At the controller-

 

 

function new_post(){

$image_codes = $_POST['image_codes'];

$field_data = $_POST['info'];

$post_body = $_POST['post_body'];

$this->load->model('blog_builder');

$ordered_data = $this->blog_builder->sort_field_data($image_codes, $field_data, $post_body);

$constructed_post = $this->blog_builder->build_post($ordered_data);

$return_array['error'] = '';

$return_array['success'] = true;

$return_array['html_data'] = $constructed_post;

echo json_encode($return_array);

exit();

}

 

At the model-

 

function sort_field_data($image_codes, $field_data, $post_body){

 

$post_bodyHTML = $post_body;

$field_data = json_decode($field_data);

if ($image_codes != false){

$image_codes = json_decode($image_codes);

foreach ($image_codes as $key=>$value){ // GET THE IMAGE CODES AND STICK 'EM IN A NICE, ORDERED ARRAY

$i = $key + 1; $code = $value;

$image[$i]['code'] = $code;

}

} else { $image = false; }

 

foreach($field_data as $key=>$value){ // FIND FIELD DATA RELATING TO IMAGES AND ADD TO ARRAY

if(is_numeric(substr($key, -1))){

$field_id = preg_replace("/[\d]/","",$key); // all letters

  $img_id = preg_replace("/[\D]/","",$key); // all numbers

$image[$img_id][$field_id] = $value;

}

elseif($key == 'title'){

$post_title = $value; // THEN GET THE TITLE, SUBTITLE & BODY, AND STORE THEM IN VARS

}

elseif($key == 'sub_title'){

$post_subTitle = $value;

}

}

 

$ordered_data = array(

'image_data' => $image,

'title' => $post_title, // AND PUT THE WHOLE LOT IN ONE ARRAY

'post_subTitle' => $post_subTitle,

'post_bodyHTML' => $post_bodyHTML

);

 

return $ordered_data;

}

 

function build_post($post){

if ($post['image_data'] == false){

return 'no images.';

} else {

return 'yep, there\'s images';

}

}

 

The data being passed is like this (for example)-

 

$image_codes = false;

$field_data = '{"title":"this is a title","sub_title":"this is a subtiutle"}';

$post_body = 'blah blah';

 

Thanks for your help, I expect it's something ridiculously obvious but this really is baffling me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.