How i can show message between variables? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have variables such as :
$type1=10;
$type2=40;
$type3=70;
.
.
.
and more
but this variables not stable because i get them from forms.
And also I have one variable :
$total=30;
I want to
if `$total` less than `$type1` =>show special message.
if `$total` between `$type1` & `$type2` =>show special message2.
if `$total` between `$type2` & `$type3` =>show special message3.
and more...

$type1=10;
$type2=40;
$type3=70;
$total=30;
$n=10;//count of variables you have
for ($i=0; $i <= $n; $i++) {
$v = $GLOBALS['type'.$i];
$nv = (isset($GLOBALS['type'.$i+1])?isset($GLOBALS['type'.$i+1]):false);
if($total < $v){
echo '$total is lesser than $type'.$i;
}elseif(isset($nv) && $v < $total && $total < $nv){
echo '$total is between $type'.$i.'and $type'.$i+1;
}
}

What's wrong with the usual if-statements? If you're getting the values from a form, you can simply assign those values to different variables and use that in your if statements to make comparisons.
$type1= $_POST['type3'];
$type2= $_POST['type3'];
$type3= $_POST['type3'];
if($total < $type1) {
//show special message
}
if($total < $type2 && $total1 > $type1) {
//show special message 2
}
if($total < $type3 && $total > $type2) {
//show special message 3
}
if(...) {
...
}

Related

Display 10 array cards [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://plnkr.co/edit/06lu6r34eGNfRq1fygNy
I want to only display 10 "cards" from my array, how would I do so?
I tried using unset() but it is very inefficient and doesn't even work for multiple cards.
Also how can I make the code display the "cards" horizontally instead of vertically?
If you’d like to use unset, the correct syntax would be unset($cards[1]); unset($cards[2]); unset($cards[3]); ... and so on.
However, for your particular situation, I’ll recommend you to use array_slice:
$cards = array(
"Messi", "Ronaldo", "Ibrahimovic", "Ribery", "Robben", "Neymar", "Rooney", "Casillas",
"Falcao", "Van Persie", "Hazard", "Iniesta", "Xavi", "Schweinsteiger", "Silva", "Fabregas",
"Lahm", "Aguero", "Cavani", "Vidic", "Ozil", "Mata", "Bale", "ThiagoSilva",
"Kompany", "Tevez", "Toure", "Ramos", "Suarez", "Pirlo", "DiMaria", "Neuer",
"Pique", "Buffon", "Lewandowski", "Gomez", "Chiellini", "Cole", "Pedro", "Busquets",
"Cech", "Muller", "Hummels", "Alonso", "Navas", "Modric", "Cazorla", "Gotze",
"Benzema", "Vidal", "Lavezzi"
);
shuffle($cards);
$cards = array_slice($cards, 0, 10);
For the horizontal display, you may simply omit the <br> at the end of every iteration of your loop, but, depending on the resolution of the user’s browser, the images may occupy more than one line. For a strictly horizontal arrangement, use an HTML table with 10 columns:
print("<table><tr>");
foreach($cards as $card){
$img = "http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/".$card.".png";
print("<td><img src=\"".$img."\"/></td>");
}
print("</tr></table>");
The following works too:
for ($i = 0; $i < 10; $i++) {
echo $cards[$i] ." <br>"; // put the name above the card
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'> <br>";
}
If you want them horizontally, and without names, don't put in the <br>:
for ($i = 0; $i < 10; $i++) {
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'>";
}

PHP objects with count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can i do something like this?
class record
{
public $count;
}
$i = 0;
foreach($entry as $item) {
$i++;
$record$i = new record();
$record$i->count = $item['count'];
print $page$i;
}
Specifically, this part here is giving me errors.
$record$i = new record();
Note: the loop works fine if i just do print $item['count']
The error is: Parse error: syntax error, unexpected T_VARIABLE
see http://3v4l.org/lB4sR
${'record'.$i};
you can create a string that holds a varname and use it as a variable later - see the example
**Edit: but like #h2ooooooo sais in the comment to your question: use an array.
Hope is that what you asking, but I do not understand why to use such bad methods to count...
class record
{
static $countq = 0;
public function count(){
self::$countq++;
}
}
$entry = array(1, 4, 6, 7);
$obj = new record();
foreach($entry as $item) {
$obj->count();
}
echo record::$countq;

What could i use to make a while statement happen only if part of it were true [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this while statement pulling information from an array. I only want it to list the entry if one of the array item $history_type does not equal "fee".
Here is what i have so far
$i = 0;
while ($i <= 9):
$history_date = $history[result][$i][Date];
$history_date_format = gmdate("m-d-y", $history_date);
$history_type = $history[result][$i][Type];
if($history_type == 'spent'):
$history_type = 'Buy';
elseif($history_type == 'earned'):
$history_type = 'Sold';
elseif ($history_type == 'fee'):
$history_type = 'Fee';
else:
$history_type = 'Error';
endif;
$history_usd = $history[result][$i][Balance][value];
$history_btc = $history[result][$i][Trade][Amount][value];
$history_amt = $history[result][$i][Value][value];
$history_rate = round($history_amt / $history_btc,2);
echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';
$i++;
endwhile;
After defining $history_type add a line:
if($history_type == 'fee') continue;
continue; statement moves to the end of current while step.
PS. You may want to move your $i++ to top of that statement, as it will get skipped too.
replace your:
echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';
with:
if($history_type != 'Fee'):
echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';
endif;
That says, in this loop iteration, if the $history_type doesn't equal 'Fee' then echo '.....'

I`m sure is a quick fix, but it does not seem to work like this [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is that the $prenume echoing works, while $comentarii doesn`t. I tried with and without quotes. If I echo $comentarii in the while loop, it echoes. I only have 1 line of text to retreive from database. Please help! Thank you !
$gaseste_elevul = "SELECT prenume_elev, comentarii
FROM elevi
WHERE kod_utilizator=1";
$gaseste_elevul_query = mysql_query($gaseste_elevul);
while($elevul = mysql_fetch_array($gaseste_elevul_query))
{
$prenume = $elevul['prenume_elev'];
$comentarii = $elevul['comentarii'];
}
if($comentarii = NULL)
{
echo "Momentan nu aveti informari pentru $prenume!";
}
else
{
echo "Mai jos aveti informarile pentru $prenume:";
echo "$comentarii";
}//sfarsit else
I also tried to do the whole if($comentarii=NULL) in the while loop, to no result.
Change
if($comentarii = NULL)
to
if($comentarii == NULL)
Your first statement sets the $comentarii to null, and that's why it doesn't echoing anything.

PHP: Basic loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I recently started to try to teach myself PHP. I've only taken a beginner class in C before, so this is a little new to me.
I was fiddling around with some basic code just to practice:
<?php
$num = 0;
while ($num < 5)
{
if ($num == 1)
{
echo 'There is' . $num . ' monkey.';
}
else
{
echo 'There are ' . $num . ' monkeys.';
$num++;
}
}
?>
However, it won't run and Chrome asks me if I would like to kill the page.
Did I create an infinite loop somehow without realizing it?
Thank you!
You did create an infinite loop; you forgot to include $num++ in the original if statement (it's only in the else, so the execution gets stuck at 1).
This is a better way:
<?php
$num = 0;
while ($num < 5)
{
if ($num == 1)
{
echo 'There is' . $num . ' monkey.';
}
else
{
echo 'There are ' . $num . ' monkeys.';
}
$num++;//moved outside the if statement
}
?>

Categories