This is working but dont seems the right way to code
I have no idea about what to do
foreach ($data as $tag) {
if ($tag==1) echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
if ($tag==2) echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
if ($tag==3) echo "<a onclick=\"window.location.href='/buscatag/3'\">Peitos grandes</a>,\n";
if ($tag==4) echo "<a onclick=\"window.location.href='/buscatag/4'\">Nudez</a>,\n";
if ($tag==5) echo "<a onclick=\"window.location.href='/buscatag/5'\">Seinen</a>,\n";
if ($tag==6) echo "<a onclick=\"window.location.href='/buscatag/6'\">Violência<a/>,\n";
if ($tag==7) echo "<a onclick=\"window.location.href='/buscatag/7'\">Vida Cotidiana</a>,\n";
if ($tag==8) echo "<a onclick=\"window.location.href='/buscatag/8'\">Harém</>,\n";
if ($tag==9) echo "<a onclick=\"window.location.href='/buscatag/9'\">Ação</a>,\n";
if ($tag==10) echo "<a onclick=\"window.location.href='/buscatag/10'\">Shonen</a>,\n";
if ($tag==11) echo "<a onclick=\"window.location.href='/buscatag/11'\">Super Poderes</a>,\n";
if ($tag==12) echo "<a onclick=\"window.location.href='/buscatag/12'\">Aventura</a>,\n";
if ($tag==13) echo "<a onclick=\"window.location.href='/buscatag/13'\">Fantasia</a>,\n";
if ($tag==14) echo "<a onclick=\"window.location.href='/buscatag/14'\">Loli</a>,\n";
}
// Define $lookup as
$lookup = [1 => 'Coméda', 2 => 'Ecchi',]; // etc
// Note the relation between key and value
foreach ($data as $tag) {
echo "<a onclick=\"window.location.href='/buscatag/{$tag}'\">" . $lookup[$tag] . "</a>,\n";
}
in code you can immediately put the number into a tag
try
$names = ['name1', 'name2'...];
foreach ($data as $tag) {
echo sprintf("<a onclick=\"window.location.href='/buscatag/%s'\">%s</a>,\n", $tag, $names[$tag-1]);
}
When you're going to test the same variable many times you can use a switch to make it better:
foreach ($data as $tag) {
switch ($tag) {
case 1:
echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
break;
case 2:
echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
break;
}
}
Related
I'm Trying to format the output of an array using php but I can't seem to get the keys and values on the same line. I've listed the code that I'm using to display the keys and values but this code outputs the keys and values on different lines
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>', $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>', $item, '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
Use <ul> <li> .... </li></ul>. Also remove comma(,) because PHP use dot(.) for concat string.
function olLiTree($tree)
{
echo '<ul>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<li>'. $key ;
olLiTree($item);
echo '</li>';
} else {
echo '<li>' .$item. '</li>';
}
}
echo '</ul>';
}
print(olLiTree($results));
You use comma , instead of dot .
for string concatenation php use dot
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>'. $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>' . $item. '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
For a multiple choice quiz application i would like to show the dummy answers with the correct answer. But with the correct answer being in a different position at each different question.
This is what i've tried but it doesn't seem to be working:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$dummy_num = 1;
foreach ($query->result() as $row)
{
$rand_number = rand(1, 3);
if ($dummy_num == $rand_number)
{
$dummy_num = $rand_number + 2;
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
//Now echo the real answer
echo '<h4>Answer '.$rand_number.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>'; //Get id's for each.echo $row->id;
}
else
{
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
$dummy_num++;
}
}
echo '</div>';
echo ' <hr/>';
}
?>
You should use shuffle function.
In your case it will be:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$answers=$query->result();
shuffle($answers);
foreach ($answers as $nr=>$row)
{
echo '<h4>Answer '.($nr+1).'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
}
echo '</div>';
echo ' <hr/>';
}
?>
Put the answers in an array the use shuffle
$random_array = shuffle($answers);
All you need to do is put the keys for your answers in an array and call shuffle(). Something like this:
$keys = array_keys($answers);
shuffle($keys);
for ($key in $keys) {
echo $answers[$key];
}
I would suggest putting all the answers into an array and using the shuffle() function to randomize them. Once they're shuffled, just iterate the array with a loop and build the markup.
You could put the results into an array (1 correct and 3 incorrect), then shuffle, then output them?
$answers = array();
array_push($answers, "answer1");
array_push($answers, "answer2");
array_push($answers, "answer3");
array_push($answers, "answer4");
shuffle($answers);
foreach ($answers as $answer) {
echo $answer;
}
i have this part of a code and i cant understund why the second loop inside the first does not work. I query two tables and i placed an echo inside the second table to see if it echo's but it does not either . thanx in advance
while($row = mysql_fetch_array($result))
{
echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>". $row['onomaselidas'] ."</span></a>\n";
echo "<ul class=\"pn2\">\n";
$idd[]=$row['idwebsiteprimary'];
while($row2 = mysql_fetch_array($result2))
{
echo "test";
if($idd[$url]==$row2['idwebsite'])
{
echo "<li class=\"s01\"><span>". $row2['name'] ."</span></li>\n";
}
}
echo "</ul>\n";
}
After the first iteration of the first loop, the internal pointer of the second result set is at the end, so the second loop will not execute, because mysql_fetch_array() will return false immediately. If you want to it exactly as above - the second result set is not dependant on the first - you will need to do this:
// First, get the results of set 2 into an array
$resultset2 = array();
while ($row = mysql_fetch_assoc($result2)) $resultset2[] = $row;
while ($row = mysql_fetch_assoc($result)) { // Do your thang
echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>".$row['onomaselidas'] ."</span></a>\n";
echo "<ul class=\"pn2\">\n";
$idd[] = $row['idwebsiteprimary'];
foreach ($resultset2 as $row2) { // Foreach sets it's pointer to the beginning every time, so this should work
echo "test";
if ($idd[$url]==$row2['idwebsite']) {
echo "<li class=\"s01\"><span>". $row2['name'] ."</span></li>\n";
}
}
echo "</ul>\n";
}
Alternatively, you can reset the internal pointer of $result2 on each iteration of the first loop by calling mysql_data_seek($result2, 0); like this:
while ($row = mysql_fetch_array($result)) {
echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>". $row['onomaselidas'] ."</span></a>\n";
echo "<ul class=\"pn2\">\n";
$idd[] = $row['idwebsiteprimary'];
mysql_data_seek($result2, 0);
while ($row2 = mysql_fetch_array($result2)) {
echo "test";
if($idd[$url]==$row2['idwebsite']) {
echo "<li class=\"s01\"><span>". $row2['name'] ."</span></li>\n";
}
}
echo "</ul>\n";
}