I'm writing a simple gamePage. My Controller method looks like this:
$model = new Game143();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->verifyAnswers();
return $this->render('Game143-confirm', ['model' => $model]);
} else {
$model->initGame(1, 2);
return $this->render('Game143', ['model' => $model]);
}
The else is the important bit. The initGame method is passed the starting level and the max number of levels, so I'm iterating 2 times with random digits and everything works fine.
In my init method I set the value:
public function initGame($level, $numEx) {
$this->maxLevel = $numEx; // normally 10
$this->thisLevel = $level; //normally 1
I also did some output. This works fine and my variable is saved as 2.
In the if method of the controller my variable is suddenly set to NULL?!
Because of this even my results are NULL, because my iteration is <=maxLevel. Anyone know why this happens?
In verifyAnswers(); my given and initialized maxLevel is NULL.
Iterating not possible:
for(; $this->thisLevel <= $this->maxLevel; $this->thisLevel++) {
If i hardcode the maxLevel everything works fine also.
In the view (Working with yii-2):
for($model->thisLevel = 1; $model->thisLevel <= $model->maxLevel; $model->thisLevel++) {
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit1[$model->thisLevel]");
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit2[$model->thisLevel]");
$out .= "\n";
$out .= "\n";
$out .= "<h3>Level $model->thisLevel</h3>\n";
$out .= "<h3>max $model->maxLevel</h3>\n";
$out .= "\n";
$out .= "<div class=\"row aufgabenFeld\">";
$out .= "<div align=\"right\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">";
$out .= $model->digit1[$model->thisLevel];
$out .= "</div>";
$out.="<div id='myField__<?php echo $model->thisLevel; ?>' class=\"col-lg-2 col-md-2 col-xs-2 wall\">\n";
$out .= $form->field($model, "proposal[$model->thisLevel]")->label(false);
$out .= "</div>";
$out .= "<div align=\"left\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">"; //id test xD
$out .= $model->digit2[$model->thisLevel];
$out .= "</div>";
$out .= "</div>";
$out .= "<div><br></div>";
In the 'submitView':
foreach ($model->proposal as $key=>$value){
$out .= "<li><h4>Aufgabe $key</h4>";
$out .= $model->digit1[$key];
$out .= " ";
$out .= $model->proposal[$key];
$out .= " ";
$out .= $model->digit2[$key];
$out .= $model->maxLevel[$key];
$out .= "</li>";
}
Related
How do I make a NxM table where each cell has two rows and its second row has two columns, but by using rowspan or colspan? I want to make something like this:
Here is my attempt.
Artikal.php:
public function get_html()
{
$html = "";
$html .= "<td>";
if (isset($this->sifra) && isset($this->naziv) && isset($this->cena) && isset($this->kolicina)) {
if ($this->kolicina > 0) {
$html .= "<img src=\"images/" . str_replace(" ", "-", $this->naziv) . jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
} else {
$html .= "<img src=\"images/none.png\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
}
} else {
$html .= "<img src=\"images/none.jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">X</span>";
$html .= "<span class=\"cena\">X</span>";
}
$html .= "</td>";
return $html;
}
index.php
if (isset($artikli)) {
for ($i = 0; $i < 6; $i++) {
echo "<tr>";
for ($j = 0; $j < 9; $j++)
echo $artikli[$i * 9 + $j]->get_html();
echo "</tr>";
}
}
I'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";
I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.
Is there anyone who can help me out with this.
It’s about a marquee shortcode in wordpress.
The marquee code shows uploaded images on date order.
I was wondering whether I could make the images slide randomly.
So that no matter what date I upload a picture, with every page view , the order of sliding images will change every time.
Here's the code:
$out .= '" id="_'.$menu_id.'" style="background-color:'.$background_color.'">'."\n";
$out .= ' <div class="shadow"></div>'."\n";
$out .= ' <div class="content">'."\n";
$out .= ' <div class="text">'.balanceTags($content).'</div>'."\n";
$out .= ' <div class="marquee-outer">'."\n";
$out .= ' <ul class="marquee-inner">'."\n";
$images_array = explode(',', $images);
foreach($images_array as $image_id){
$image = wp_get_attachment_image_src($image_id, 'full');
$out .= ' <li class="marquee-item" style="background-image:url('.$image[0].');"></li>';
}
$out .= ' </ul>'."\n";
$out .= ' </div>'."\n";
//$out .= ' </div>'."\n";
//$out .= '</section>'."\n";
return $out;
}
Any ideas?
Use the PHP shuffle function to randomise the array:
$out .= '" id="_'.$menu_id.'" style="background-color:'.$background_color.'">'."\n";
$out .= ' <div class="shadow"></div>'."\n";
$out .= ' <div class="content">'."\n";
$out .= ' <div class="text">'.balanceTags($content).'</div>'."\n";
$out .= ' <div class="marquee-outer">'."\n";
$out .= ' <ul class="marquee-inner">'."\n";
$images_array = explode(',', $images);
shuffle($images_array);
foreach($images_array as $image_id){
$image = wp_get_attachment_image_src($image_id, 'full');
$out .= ' <li class="marquee-item" style="background-image:url('.$image[0].');"></li>';
}
$out .= ' </ul>'."\n";
$out .= ' </div>'."\n";
//$out .= ' </div>'."\n";
//$out .= '</section>'."\n";
return $out;
}
trying to return $out
$out = '';
$out .= '<form id="agp_upload_image_form" method="post" action="" enctype="multipart/form-data">';
$out .= wp_nonce_field('agp_upload_image_form', 'agp_upload_image_form_submitted');
$out .= $posttile = get_option('posttitle');
$out .= $postdiscription = get_option('postdiscription');
$out .= $postauthor = get_option('postauthor');
$out .= $postcategory= get_option('postcategory');
$out .= $uploadimage= get_option('uploadimage');
$out .= $posttitleenabledisables = get_option('posttitleenabledisables');
$out .= $postdiscriptionenabledisable = get_option('postdiscriptionenabledisable');
$out .= $postauthorenabledisable = get_option('postauthorenabledisable');
$out .= $postcategoryenabledisable = get_option('postcategoryenabledisable');
$out .= $uploadimageenabledisable = get_option('uploadimageenabledisable');
$out .= $posttaxonomies = get_option('posttaxonomies');
$out .= $enablecaptcha = get_option('captchaprivatekey');
if ($posttitleenabledisables == 'disable') { } else {
$out .= '<label id="labels" for="agp_image_caption">"'.if ( isset($posttile[0])) { echo get_option('posttitle'); } else { echo 'Post Title'; } .'":</label><br/>';
$out .='<input type="text" id="agp_image_caption" name="agp_image_caption" value="$agp_image_caption ;"/><br/>';
}
but stuck at this point giving error
$out .= '<label id="labels" for="agp_image_caption">"'.if ( isset($posttile[0])) { echo get_option('posttitle'); } else { echo 'Post Title'; } .'":</label><br/>';
i want this return but getting error because i think variable not allow if else
can some one tell how to fix this
The way you're trying to use if/else, I assume you need a ternary operator. If/else control cannot be used the way you are trying.
Try:
$out .= '<label id="labels" for="agp_image_caption">"'. ( isset($posttile[0]) ? get_option('posttitle') : 'Post Title' ) .'":</label><br/>';
Basically, if-else does not return any value and cannot be used inline. The ternary operator evaluates to a single value and can be concatenated with strings and used inline in any other expression.
$cond ? $true_val : $false_val
If $cond evaluates to true, the entire statement evaluates to $true_val, otherwise $false_val.
Ty Ternaryoperator.
$out .= '<label id="labels" for="agp_image_caption">"';
$out.= (isset($posttile[0]))? get_option('posttitle') : 'Post Title';
$out.='":</label><br/>';
This is because you have a condition in the string concat.
replace
$out .= '<label id="labels" for="agp_image_caption">"'.if ( isset($posttile[0])) { echo get_option('posttitle'); } else { echo 'Post Title'; } .'":</label><br/>';
with
$caption = isset($posttile[0]) ? get_option('posttitle') : "Post Title";
$out = '<label id="labels" for="agp_image_caption">"' . $caption . '":</label><br/>';
Try like
if ( isset($posttile[0])) {
$out .= '<label id="labels" for="agp_image_caption">'. get_option('posttitle').':</label><br/>';
} else {
$out .= '<label id="labels" for="agp_image_caption">Post Title:</label><br/>';
}
In your code remove extra "
Orelse as like your code change like
$out .= '<label id="labels" for="agp_image_caption">'.if ( isset($posttile[0])) { get_option('posttitle') } else { 'Post Title' } .':</label><br/>';