how to echo array using foreach in echo - php

I have an array $category_slugs
how to output this using echo?
I have an error in code bellow:
echo "<div class='transition ".foreach($category_slugs as $slug){echo $slug;echo ' ';}."' data-category='transition'> " ?>
Thanks

You can't use foreach inside a echo. To achieve what you are trying you can use implode function, it concatenates the values of an array on a string.
echo "<div class='transition " .implode(' ', $category_slugs). "' data-category='transition'> " ?>

try this code
<div class="transition <?php foreach($category_slugs as $slug){echo $slug . ' ';} ?>" data-category="transition">
The concatenation operator ('.'), which returns the concatenation of its right and left arguments.
from php manual
But this code not a string.
foreach($category_slugs as $slug){echo $slug;echo ' ';}
So you will get a error.
This manual may help you Escaping from HTML.
Sorry for my bad english, I hope it will help you.

try this
$category_slugs =array('a','b','c');
foreach($category_slugs as $slug){
echo "<div class='transition ".$slug.' '."' data-category='transition'></div> " ;
}

Wrap <div> in a for loop, like so
<?php
$slug_string = "";
foreach($category_slugs as $slug){
$slug_string .= $slug_string." ";
}
echo "<div class='transition ".$slug_string."' data-category='transition'> ";
?>

Related

Variable in php echo

I would really need help with a primitive thing.
I need this:
<?php echo do_shortcode('[cycloneslider id="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo $my_meta['odkaz']; ?>"]'); ?>
I do not know how exactly do it.
I made it by this solution:
<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);?>
<?php echo do_shortcode('[cycloneslider id="'.$my_meta['odkaz'].' "]'); ?>
Is it right or..Could you edit it to right way?
I suppose you don't see clear your own code.
If that happens, then adopt a more verbose way of programming and create variables that describe more the values:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$idCycloneSlider = $my_meta['odkaz'];
$doShortCodeResult = do_shortcode('[cycloneslider id="'.$idCycloneSlider.'"]');
echo $doShortCodeResult;
?>
That way we see clearly what happens in each line and the string concatenation is more simple to do.
Concatenate string and variables with the . operator:
$var = "Hello, I am made up of " . $some . $variables . " and strings";
You can write it using a string interpolation:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo do_shortcode("[cycloneslider id=\"{$my_meta['odkaz']}\"]");
?>
Or using a heredoc if extra spaces are ok:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$shortcode = <<<TEXT
[cycloneslider id="{$my_meta['odkaz']}"]
TEXT;
echo do_shortcode($shortcode);
?>

i need fix select form on php code

Hello I need to fix a problem on my PHP code. When I write the value name="" and id="" aren't found. Here's the code:
<?php
$tipos= $eachoption['option_value'];
$categorias='';
$cats = explode(",",$tipos);
echo "<select name=\"option_<?php echo $eachoption['option_id'];?>[]\" id=\"<?php echo $_POST['option_'.$eachoption['option_id']][$i];?>\">";
foreach($cats as $cat){
$cat = trim($cat);
$categorias .= "<option>". $cat ."</option>";
}
echo $categorias;
echo "</select>";
?>
Thanks! I think that maybe is for " or ' inside echo.
Your PHP syntax is incorrect. You cannot embed PHP-within-PHP. e.g.
<?php
$foo = "<?php echo 'bar' ?>";
will NOT execute that echo call. You are assigning the literal characters <, ?, p, etc... to a string.
Since you're using double-quoted strings, you don't need the echoes for simple variable insertions at all:
echo "<select name=\"option_{$eachoption['option_id']}[]\" id=\"" . $_POST['option_'.$eachoption['option_id']][$i]; . "\">";
^^^^^^^^^^^^^^^^^^^^^^^^^^
note that the second $_POST does require breaking out of string mode, since you're dynamically creating the array key.

Insert function name with php

I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';

php link system problem

<?php
$total=3;
echo '
<div class="idsdiv">.$total.<div> ';
?>
i want to appear $total variable number in the link.why is this script not working?
Enclose the whole string with double quotes to embed variables inside:
echo "<div class=\"idsdiv\">$total<div>";
You need to use double quotes around your HTML and single quotes around your attributes or do this...
echo '<div class="idsdiv">' . $total . '<div> ';
PHP doesn't process variable names in strings enclosed in single quotes.
<?php
$total=3;
echo '<div class="idsdiv">',$total,'<div>';
?>
Look at the string section of php.net (http://php.net/string) they talk about how to use each of the types. One of quote being the ' where nothing is parsed.
<?php $total=3;
echo "<div class=\"idsdiv\">$total<div> ";
?>
you had errors with your quotes
You can print HTML without printing it, like so:
<?php
$total = 3;
?>
<div class="idsdiv"><?php echo $total; ?></div>
When I still did PHP, I found it much easier to manage than escaping tons of quotes and things like that.
You can even do it inside of an if block too:
<?php
if ($foo == 'bar') {
?>
<div>Foo is bar</div>
<?php
}
?>
The method I like is
<?php
$total=3;
echo "<div class='idsdiv'><a href='profile.php?id={$total}'>{$total}</a><div>";
?>
It is my method, but there are plenty of ways to do it. Maybe even too many. If you want more information you can always refer to the documentation.
<?php
$total=3;
echo '<div class="idsdiv">'.$total.'<div>';
?>
You're quote are missing before and after the $total

Where is the problem in this PHP code?

There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.

Categories