My question is the opposite of this Using two values for one switch case statement
I'd like to use multiple values in the switch part of the statement, not the case part which I already know how to do.
<?php
$three = json_decode( $ex[ 'three-images' ] );
$i = 0;
$imgs = $trans = $class = $parallaxDir = [];
foreach ( $three as $img ) {
$imgs[ $i ] = $img->Image;
$parallax[ $i ] = $img->Transition;
$class[ $i ] = $img->Class;
//for ($ii = 0; $ii < 3; $ii++) {
switch ( $class[ $i ] ) {
case 'middle':
case 'onright':
$parallaxDir[ $i ] = '0';
break;
case 'top':
case 'onleft':
$parallaxDir[ $i ] = '1';
break;
default:
$parallaxDir[ $i ] = '9';
break;
};
//}
$i++;
}
?>
<div class="side image <?php echo $class[1]; ?>" <?php if(!empty($parallax[1])) { echo 'data-para="' . $parallax[1] . '"'; } ?> <?php if(!empty($parallaxMob)) { echo 'data-para-mobile="' . $parallaxMob . '"'; } ?> <?php if(!empty($parallaxDir[1])) { echo 'data-para-dir="' . $parallaxDir[1] . '"'; } ?>>
<img class="parallax" src="<?php echo $imgs[1]; ?>" alt="<?php echo explode('[',trim($item->title)[0]); ?> image"/>
</div>
This is the full code, seems it was working fine as I had it but for some reason when trying to implement it later down the line the array values were returning as empty even though they weren't.
So once I took the [1] off the parallaxDir if(!empty) check it started working
No, not possible.
Though you could do something like this:
foreach([$class[0], $class[1]] as $value) {
switch($value) {
// case...
}
}
But you are probably better off using if statements.
Related
I am working on a website in which I want to hide text (Hello World, Point A, and Point B) when serialized string is empty. Below is my code:
<p class="font-weight-bold">Hello World</p>
<p class="font-weight-bold">Point A</p>
<?php
$serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0)
{
$serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($serialized != '')
{
$unserialized = unserialize( $serialized );
foreach($unserialized as $key=>$value)
{
echo $key." ".$value['start']." ".$value['end']."<br/>";
}
}
?>
<p class="mt-2 font-weight-bold text-center">Point B</p>
<?php
$serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0)
{
$serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($serialized != '')
{
$unserialized = unserialize( $serialized );
foreach($unserialized as $key=>$value)
{
echo $key." ".$value['start']." ".$value['end']."<br/>";
}
}
?>
Problem Statement:
Inside Hello World, I have two sections - Point A and Point B.
If both Point A and Point B are empty (meaning serialized string is empty) and there is no data to show then I don't want to show <p> text for Hello World, Point A and Point B at all.
But if either of them is present (meaning serialized string is not empty) then I want to show <p> text for Hello World and <p> text for whatever it is present (it can be Point A or Point B).
Is this possible to do? When the serialize string is empty it is displaying like this:
I don't want those texts to print when the serialized string is empty for Point A or Point B.
You should put the html output in several vars (including the 'Hello World') and do the output at the end when you did all the testing.
<?php
// pre-set/initialize some html templates/strings to be used later
$html = '';
$helloWorld_header = '<p class="font-weight-bold">Hello World</p>';
$pointA_header = '<p class="font-weight-bold">Point A</p>';
$pointA_content = '';
$pointB_header = '<p class="mt-2 font-weight-bold text-center">Point B</p>';
$pointB_content = '';
$pointA_found = $pointB_found = false; // initialize both to false
$pointA_serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0) {
$pointA_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($pointA_serialized != '') {
$pointA_found = true;
$unserialized = unserialize( $pointA_serialized );
foreach($unserialized as $key=>$value) {
$pointA_content .= $key." ".$value['start']." ".$value['end']."<br/>";
}
// add everything from pointA to $html
$html .= $pointA_header . $pointA_content;
}
$pointB_serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0) {
$pointB_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($pointB_serialized != '') {
$pointB_found = true;
$unserialized = unserialize( $pointB_serialized );
foreach($unserialized as $key=>$value) {
$pointB_content . $key." ".$value['start']." ".$value['end']."<br/>";
}
// add everything from pointB to $html
$html .= $pointB_header . $pointB_content;
}
// if pointA or pointB is set/found...
if($pointA_found || $pointB_found) {
// ... add the main header before the rest
$html = $helloWorld_header + $html;
}
// do the actual output
echo $html
?>
This is just a quick fix of what you've got. There would be more elegant ways to do that. I'd put pointA and pointB in a shared function, as you mostly do twice the same thing.
I need to know if there's a quickest and efficient way to display one or more images if $value == $string
For example: I have an cell which only contains 3 single string: 'r o g', if user put ro it will output <img src="red.gif"> and <img src="orange.gif"> So it could be random if user insert gr then it will display <img src="green.gif"> and <img src="red.gif">
Right now I can only think something like...
<?php $red = "<img src="red.gif">";
$orange = "<img src="orange.gif">";
if( $cell1 == $red ){ echo $red;}
if( $cell1 == $red && $orange ){ echo $orange.$red;}
etc...
This method might works but has to provide too many possibility and I believe there's a shorter and efficient to do that, but I haven't got any idea because still I'm learning PHP
How about this approach?
<?php
//define all your images here
$images = [
'r' => 'red.png',
'g' => 'green.png',
'b' => 'blue.png',
'y' => 'yellow.png',
'o' => 'orange.png'
];
function output($input, $images) {
$parts = str_split($input);
foreach ($parts as $part) {
if (isset($images[$part]))
echo '<img src="' . $images[$part] . '">';
}
}
echo "rgb: \n";
output('rgb', $images);
echo "\n\nyor: \n";
output('yor', $images);
echo "\n\nxxy: \n";
output('xxy', $images);
Output:
rgb:
<img src="red.png"><img src="green.png"><img src="blue.png">
yor:
<img src="yellow.png"><img src="orange.png"><img src="red.png">
xxy:
<img src="yellow.png">
Try this approach:
for( $i = 0; $i <= strlen( $cell1 ); $i++ ) {
$char = substr( $cell1, $i, 1 );
switch ($char) {
case "r":
echo '<img src="red.gif">';
break;
case "o":
echo '<img src="orange.gif">';
break;
case "g":
echo '<img src="green.gif">';
break;
default:
break;
}
}
Here is the code that shows an example for 3 character inputs. $string can be the posted value.
$r = '<img src="red.gif">';
$y = '<img src="yellow.gif">';
$o = '<img src="orange.gif">';
$g = '<img src="green.gif">';
$string = 'ryo';
$length = strlen($string);
for ($i=0; $i<$length; $i++) {
echo ${''.$string[$i]};
}
I am trying to make a text replacer but since there are letters repeated i keep getting things i dont want. Does anyone know how i can do this?
Input
function image($img) {
$img = ereg_replace("a","<img src=r/a.png>", $img);
$img = ereg_replace("b","<img src=r/b.png>", $img);
$img = ereg_replace("c","<img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Output
<img sr<img src=r/c.png>=r/a.png><img sr<img src=r/c.png>=r/b.png><img src=r/c.png>
Output I Want
<img src=r/a.png><img src=r/b.png><img src=r/c.png>
Try this, may be insufficient but it will satisfy your requirement:
function image($img) {
$data="";
for( $i = 0; $i <= strlen($img); $i++ ) {
$char =substr( $img, $i, 1 );
switch($char)
{
case 'a':
$data .="<img src=r/a.png>";
break;
case 'b':
$data .="<img src=r/b.png>";
break;
case 'c':
$data .="<img src=r/c.png>";
break;
default:
break;
}
}
return $data;
}
$img = "abc";
echo image($img);
Problem with "c" your "a" and "b" replacing correctly but when it reach "c" there are many "c" cause "src" also added so it replaces all "c"
try with single statement
function image($img) {
$img = ereg_replace("abc","<img src=r/a.png><img src=r/b.png><img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Also ereg_replace() has been deprecated
Here is what I tried:-
function image($img) {
for($i=0;$i < strlen($img);$i++){
$letterarray[]=$img[$i];
}
$a=0;
foreach ( $letterarray as &$value) { // reference
$value= str_replace($value, "<img src=r/$value.png>", $value);
$a++;
$ab[] = $value;
}
return implode("",$ab);
}
$img = "abc";
echo image($img);
Here in the function image(), you don't need to specify the alphabets contained in $img
I have a function:
function getDecision($num_players, $decisionType, $stage){
echo "in the function 1: ".$num_players."<br/>";
echo "in the function 2: ".$decisionType."<br/>";
echo "in the function 3: ".$stage."<br/>";
$x = mysql_query("
SELECT `decisionValue` FROM `teamdecision` WHERE `decisionType` = '$decisionType' && `period`= '$stage'
")or die($x."<br/><br/>".mysql_error());
$y = array();
$i="0";
while ($i<($num_players) && $row = mysql_fetch_assoc($x))
{
$y[$i] = $row['decisionValue'];
$i++;
}
return ($y);
}
Y will be populated with a number of values depending on the $num_players. I am calling the function as follows:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
As you can see I am doing this wrong. I need to output the data located in $value however I am confused with how multi dimensional arrays work. I understand I need to read up on the subject, if possible can you point me to a suitable learning source?
So I changed up the code so that the following is being used to produce an output below:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
I expected to output as follows:
$value[0] should contain $y which then contains each SUconsultant value. $value[1] should contain $y which then contains each marketT value and so on.
for ($i=0;$j<4;$i++){
should be
for ($j=0;$j<4;$j++){
And
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
should be
$value[$j] = getDecision($num_players, $name[$j], $currentStage)."<br/>";
You have a $j in your first for loop, but setting and incrimenting $i in it. Maybe change them to $j as well.
EDIT
It actually looks like you have an issue with where you are returning the data:
echo "Value = ".$value[$j][$i]."<br/>";
This should be (it appears):
for ($f = 0; $f < $num_players; $f++){
echo "Value = ".$value[$i][$f]."<br/>";
}
Since you are currently on $value[$i] by the time you reach this point, then you need it to echo out one for each player.
This solution worked, after a lot of playing around it seems that the [] after of
the value array was also needed? At least I think so
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[] = getDecision($num_players, $name[$i], $currentStage);
//echo "Value = ".$value[$i]."<br/>";
echo "<br/><hr/>";
echo "Value = ".$value[$i][$j]."<br/><hr/>";
}
}
Output:
And now I have changed to:
$value[] = getDecision($num_players, $name[$i], $currentStage);
echo "Team ".$j." ".$name[$i]." = ".$value[$i][$j]."<br/>";
To get my eventual goal of:
I am new this parsing xml feeds; so I have a piece of code that I have been working on that extracts the data from the xml file. Now I want be able to sort the array by the publish date pubdate. I have not found any examples that can anyone help me.
I used sort() which puts it in some random order.
$xml_headline_key = "*ARRAY*CATCHUP*PROGRAMMENAME";
$xml_link_key = "*ARRAY*CATCHUP*ITEMURL";
$xml_description_key = "*ARRAY*CATCHUP*SHORTSYNOPSIS";
$xml_publish_key = "*ARRAY*CATCHUP*ORIGINALAIRINGDATE";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description, $link, $pubdate;
}
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_link_key, $xml_description_key, $counter, $story_array, $xml_link_key, $xml_publish_key;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_link_key:
$story_array[$counter]->link = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
case $xml_publish_key:
$story_array[$counter]->pubdate = $data;
break;
}
}
$corrie_counter = 0;
sort($story_array);
$dateformat = "D j M, g:ia";
for($x=0;$x<count($story_array);$x++){
if($story_array[$x]->headline == "Coronation Street"){
if($corrie_counter != 4){
echo "<li><span class=\"bold\">". date($dateformat, strtotime($story_array[$x]->pubdate)) . "</span>\n";
echo "\t<span class=\"text\">" . trunc($story_array[$x]->description,30, " ") . "</span>\n";
echo "\t";
$corrie_counter++;
}
}
}
You can use uasort, and inside sortByPubdate define the way the two pubdates should be compared.
uasort($story_array, 'sortByPubdate');
// Sort function
function sortByPubdate($a, $b) {
if ($a->pubdate == $b->pubdate) {
return 0;
}
return ($a->pubdate < $b->pubdate) ? -1 : 1;
}