i have an array and i want to convert this array in comma seprated string by implode function but this is not working. my code is below.
<?php foreach ($article['hashtags'] as $hashtags) { ?>
<?php $hastagg=mysql_real_escape_string(implode(',',$hashtags)) ?>
<a><?php echo $hastagg; ?></a>
<?php } ?>
Have you tried this:
<?php
$hastagg = htmlentities(implode(',' ,$article['hashtags'])) ;
echo '<a>'.$hastagg.'</a>';
?>
I don't know about structure of $article array. But I have a feeling that foreach is unnecessary:
$hashtagg = implode(',', $article['hashtags']);
And why are you using mysql_real_escape_string for output? You can use htmlspecialchars or some other functions.
Related
Here is the code used to get the ids of a images in a gallery
<?php $images = get_field('photogallery');?>
<?php foreach( $images as $image ): ?>
<?php echo $image['ID']; ?>
<?php echo ','; ?>
<?php endforeach; ?>
I get the output
1102 , 3380 , 3348 , 3354 , 3355 ,
I would like to get this outside the loop because the result must be used in other shortcode also I see there is a whitespace after every number.
the result must be
1102,3380,3348,3354,3355
Please help me.. thanks
You don't need to put <?php ... ?> everytime everywhere for each statement. Keep in mind that each time you close with ?> all characters are sent to the client until the next opening <?php, that's why you obtain spaces around each comma:
<?php foreach( $images as $image ): ?>#
#####<?php echo $image['ID']; ?>#
#####<?php echo ','; ?>#
<?php endforeach; ?>
(I changed white-spaces to #, this way you can see characters sent to the client (the browser)).
You can use array_map to "filter" only ID items and implode to join them , then you only need to store the result in a variable ($result here).
<?php
$images = get_field('photogallery');
$result = implode(',', array_map(function ($i) { return $i['ID']; }, $images));
echo $result;
?>
Now you can use $result later everywhere you want.
Much simpler:
<?php echo implode(',' array_column($images, 'ID')); ?>
extract the ID values using array_column()
implode() those array values using a comma
So I have this foreach loop that looks like this:
Well, I need to pass into the same foreach loop another variable that unfortunately I can't get it through the getCategories() function;
So I have another function with another variable, that is passed through this template by a controller from a class function, is there a way I can use another variable into the same foreach? e.i:
Example:
$template->countries=$className->functionFromClass();//returns =something;
//getCategories() returns=thing;
how do I use countries variable in the same foreach bellow?
The template:
<!--how do I add $countries variable in this foreach?-->
<?php foreach(getCategories() as $category && $countries as $country) : ?>//this is not working
<?php echo $category->name; ?>&<?php echo $country->name;?>
<?php endforeach;?>
The result that I am looking for:
thing&something
I've made research and there are indeed questions and responses about foreach but with arrays,I don't have arrays here, my variables are returned as a single word and they come from 2 different tables, can someone help me in this problem?
Try something like this:
<?php $countries = getCountries(); // function that return countries array ?>
<?php foreach(getCategories() as $i=>$category) : ?>
<?php $country = isset($countries[$i]) ? $countries[$i] : end($countries); ?>
<?php echo $category->name; ?>&<?php echo $country->name; ?>
<?php endforeach;?>
So I'm using:
<?php print $variable; ?>
and sometimes there are multiple variables being printed, but I'm not able to tell how many. Is there a way of printing the variables separated by a comma, but only when there are more than one?
Thanks!
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
<?php
var_dump($variable);
?>
if your $variable is an array count it like count($variable) and than put you condition like
if(count($variable)>1) { $imp=implode(',',$variable); }
hope this will help you..
If it's not an array
<?php /* somwhere before... */ $sep = ''; ?>
<?php print $sep.$variable; $sep = ',' ?>
If it's array
<?php print implode( ',', $variable ); ?>
recently I came across a problem where I would need to generate an object from CodeIgniter PHP foreach loop. Basically I need to filter through my email list and generate the object based on the result, then pass it to the view and use another foreach loop to list the content out there.
Here's what I have and it successfully generates an array $email_raw, but I couldn't find the right way to generate it as an object instead:
PHP:
$email_preliminary=$this->db->select('email')->get('user');
$email_raw = array();
foreach ($email_preliminary->result() as $row):
$email_to_test=$row->email;
if(filter_var($email_to_test, FILTER_VALIDATE_EMAIL)||preg_match('/\d*[1-9]\d*/', $email_to_test))
{
$email_raw[] = $email_to_test;
}
else{
}
endforeach;
People suggested that I use:
$email_raw[] = (object)array('email'=>$email_to_test);
But the error msg says its not an object.
What are other ways I can generate $email_raw as an object so I can pass it to the view and use foreach to list the content out there?
Many thanks!
Update:
per request, here's rest of the controller:
$data['record']=array_unique($email_raw);
$this->load->view('login&signup/signup_test_get_wrong_email_view', $data);
and the view I use:
<?php foreach ($record->result() as $row): ?>
<span><?php echo $row->email; ?></span><br>
<?php endforeach; ?>
foreach can iterate object and arrays. You don't have to convert your array to object.
Leave the controller like it is above:
$email_preliminary=$this->db->select('email')->get('user');
$email_raw = array();
foreach ($email_preliminary->result() as $row):
$email_to_test=$row->email;
if(filter_var($email_to_test, FILTER_VALIDATE_EMAIL)||preg_match('/\d*[1-9]\d*/', $email_to_test))
{
$email_raw[] = $email_to_test;
}
else{
}
endforeach;
// ... code
$data['record']=array_unique($email_raw);
$this->load->view('login&signup/signup_test_get_wrong_email_view', $data);
In the view you simply iterate that array you've just constructed:
<?php foreach ($record as $email): ?>
<span><?php echo $email; ?></span><br>
<?php endforeach; ?>
I guess your doubt comes from lack of knowing how the $data['record'] variable looks like. Use print_r($data['record']); right after $data['record'] is being set.
How would I get all the data from fields that have been posted or requested using get in PHP?
e.g
echo $_GET[*];
var_dump ($_GET);
or
print_r ($_GET);
or
echo (json_encode ($_GET));
or
foreach ($_GET as $key => $val)
{
echo ($key . ', ' . $val);
}
or any number of other methods
You can use print_r:
<?php
...
print_r($_GET)
...
?>
If you want to display all the values of an array or an object, you can use print_r. You'll want to use it inside <pre> tags to get all the indentation and new lines.
<pre>
<?php print_r($_GET); ?>
</pre>
extract($_GET);
fun with extract!