Values of an indexed array in select dropdown option - php

I want to get the array values in dropdown option menu.This is my code so far
<?php
$names=array("jan","feb","mar");
foreach($month_names as $mn)
{
echo "<select>";
echo "<option>$mn</option>";
echo "</select>";
}
?>
But the code is creating a new select tag for each value.
How can I Get all array value inside a select tag?
Any help is much appriciated, thanks!
Regards: Amit Perez

Please try this code, In your code you were including <select></select> inside foreach loop, that's why you were getting 3 select tag.
<?php
$names=array("jan","feb","mar");
echo "<select>";
foreach($month_names as $mn)
{
echo "<option>$mn</option>";
}
echo "</select>";

Please change your code as follows
<?php
$month_names=array("jan","feb","mar");
echo "<select>";
foreach($month_names as $mn)
{
echo "<option>$mn</option>";
}
echo "</select>";
?>

Related

Put the selectbox in php html

I want to put the select clause in php html.
here my code:
<?php
for($index=1; $index<=$content->option->dynamic_field_index; $index++){
echo "<td><select name='kboard_option_product'
id='kboard_option_product{$index}' data-placeholder='selectProduct'>";
echo "<optgroup value='0' label='products'>";
echo "<option value='windows'>windows</option>";
echo "<option value='linux'>linux</option>";
echo "<option value='unix'>unix</option>";
echo "</optgroup></select></td>";
}?>
How can I fix it?
I want to do that in
If you look at the options below you will save the data like this, but if you use echo, you have to match php syntax and html syntax.
<option value="window"<?php if($content->option->product == 'window'):?> selected<?php endif?>>window</option>
What I want to do is to use the for statement in the php clause and use the tag with echo to show it as html
Just use the following ,
function php_selectbox(){
ob_start();
?>
//Place your html here and echo the function outside.
<?php
return ob_get_clean();
}

Showing the first element in an foreach array in HTML

I have an array $category['transactions']. It stores data as follow:
ID Name Phone
1 Test Test2
2 Test3 Test4
It's because I use the same array for different purpose, at one of the scenario is to show only the first record in this array. I don't what to change the coding in php nor creating a different parameter. What can I improve based on the following coding in html to get the first record only in this array?
<?php foreach($category['transactions'] as $transaction) { ?>
<div><?php echo $transaction['id']; ?></div>
<div><?php echo $transaction['name']; ?></div>
<?php } ?>
replace your code with.
<?php $firstRow=reset($category['transactions']);
echo '<div>',$firstRow['id'],'</div>';
echo '<div>',$firstRow['name'],'</div>';
?>
You don't need to iterate through the array to get the first element.
You don't even need the foreach to get the first element. Just use array_values():
$first = array_values($category['transactions')[0]
try this..
<?php
foreach($category['transactions'] as $transaction)
{
echo $transaction['id'];
break;
}
?>
and no need to use multiple php tags...

PHP sorting 5 numbers per row

Hi I have made script that shows only even numbers and now i have to do that shows only 5 even numbers per row example:
numbers must be sorted like this
2,4,6,8,10
12,14,16,18,20
i must have numbers written like that current code that i am using for showing only even numbers is under. but if someone can help me how can i show only 5 per row i will be thankful. Thanks in advance.
<?php
$p=100;
for($p=100;$p>=0;)
{
echo "$p,";
$p=$p-2;
}
?>
Try something like this
<?PHP
$p=100;
$count=0;
for($p=100;$p>=0;)
{
echo "$p";
$p=$p-2;
$count+=1;
if($count==5){
$count=0;
echo "<br/>";
}
else echo " ,";
}
?>
One-liner:
echo implode("\n",array_map(function($a) {return implode(",",$a);},array_chunk(range(0,100,2),5)));
Result:
0,2,4,6,8
10,12,14,16,18
20,22,24,26,28
30,32,34,36,38
40,42,44,46,48
50,52,54,56,58
60,62,64,66,68
70,72,74,76,78
80,82,84,86,88
90,92,94,96,98
100
Function reference: implode, array_map, array_chunk, range.
For rendering in a browser, replace \n with <br />.
try the following
<?php for($p=100;$p>=0;$p-2){
echo "$p";
if($p%5==0){
echo "<br>";
}
else {
echo ", ";
}
}
?>

PHP MySQL Order list

Can anyone help?
My code is like this:
<ol><li>{$student_Value}</li> <ol>'
it gives result:
1. Student101Name
1. Student102Name
1. Student103Name
I want something like:
Student101Name
Student102Name
Student103Name
Please help... Thank you!
Do not close the OL tag every time
OL = Ordered List
LI = List item
If you close and reopen the OL, it creates a new ordered list, and so restarting at 1.
<pre>
echo "<ol>";
foreach loop {
echo "<li>{$student_Value}</li>";
}
echo "</ol>";
</pre>
Good luck.
If you use MySQL to get the data you can add ORDER BY to the query to order the result!
Here more info how to use ORDER BY
And change the HTML Code to be:
<ol>
<li>$student_Value</li>
<li>$student_Value</li>
<li>$student_Value</li>
</ol>
PHP Code will be:
echo "<ol>";
echo " <li> $student_Value </li> ";//write all student values like this
//more student values
echo "</ol>";
It looks like your problem is more CSS related than PHP or SQL.
The reason is probably that your output is:
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
Each student should be a list item within the ordered list:
<?php
print "<ol>";
while ($row = $result->fetch_assoc())
{
print "<li>{$row['student']}</li>";
}
print "</ol>";
?>
You need something like that
<ol>
<?php
for($i=0; $i<count($studentValue); $i++)
{
?>
<li><?php echo $studentValue[$i]; ?></li>
<?php
}
?>
<ol>
I am giving you above example to tell you that under a 'ol' tag 'li' must be multiple to get your require result.

Having trouble with echoing HTML in php script

I am trying to make a form that show users name and I am having a little trouble with this part. The ' ' and " " marks dont quite work how they are supposed to. Im trying to echo the options in drop down menu and some how the $wholenames and the last " sign appear in the wrong part of the page. Could someone please tell me what is the correct way of doing this?
Thanks
echo' "<option>'; echo $wholenames; echo'</option>"';
Actually I had looked it wrong it is a little bit more complex. Below you can see the code. The whole dropdown menu does not appear. The wholenames integer appears, but the menu does not...
echo'
<label for="addusertogroup">Add user to an existing group:</label>
<select name="addusertogroup" id="addusertogroup">
'; if(mysql_num_rows($userresult))
{
while($row2 = mysql_fetch_assoc($userresult))
{
$wholename = array("$row2[f_name] $row2[s_name]");
foreach ($wholename as $wholenames) {
echo "<option>$wholenames</option>";
}
}
}
else {
echo "<option>No Names Present</option>";
}
To make it work, simply do this:
echo "<option>";
echo $wholenames;
echo "<option>";
or this:
echo "<option>$wholenames</option>";
or this:
echo '<option>'.$wholenames.'</option>';
All will work, just up to you which one you pick.
You shouldn't have " before <option> and after </option>
It should be something like
echo "<option>". echo $wholenames; echo "</option>";
Also, if $wholenames is an array, you'd better iterate over it:
foreach ($wholenames as $name){
echo "<option>". echo $name; echo "</option>";
}
Any text for options in a HTML SELECT box are written inside the tag. If you don't put your text between the <option> tags the browser will try to insert it to the select box's DOM.
So you could change your code to this:
echo '<option value="myvalue">"' . $wholenames . '"</option>';
Haven't actually tested this code.
Update if the Quotation mark was not meant to be in the output you would simply need to write:
echo '<option value="myvalue">' . $wholenames . '</option>';
In php you can use both " " and ' ' with strings.

Categories