Im looking for a way to change the number in a class if i=4 or higher.
$i=1;
foreach($properties['blocks'] as $block)
{
echo '<div class="colum1" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
Is there any way to do this ? colum1 needs to become colum2. i've been trying to use things like if else etc. but so far no success.
$i = 1;
foreach($properties['blocks'] as $block)
{
echo '<div class="' . ($i < 4 ? 'colum1' : 'colum2') . '" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
// --------------
Related
I'm working on pagination for my website, yet I'm stuck on the following piece of code. I've been messing around with it for at least an hour, and can't seem to wrap my head around what's going on with the output.
Of course the if statement executes once.
And as expected the first echo ... returns 1.
However, for some reason the second echo ... returns 0 as a float instead of <div>1</div> as a string...
$rowCount = 5;
$pgCount = ceil($rowCount / 10);
$pgParamArray["page"] = $pgCount;
$pgArray = array("", "", "", "", "");
for ($i = 0; $i < 5; $i++) {
if ($pgParamArray["page"] - $i > 0) {
echo $pgParamArray["page"] - $i;
$pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
echo $pgArray[$i];
}
}
I have tried setting $pgArray as array() and array($v1, $v2, $v3, $v4, $5) with no luck.
Even though var_dump($pgParamArray) returns float, I tried converting $rowCount, which is initially a string from the database, to a number anyways. No dice again.
echo $pgArray["0"] also returns 0.
var_dump($pgArray[0]) also returns float.
var_dump($pgArray) obviously returns array.
However, var_dump($pgArray) returns array(5) { [0]=> string(7) ...
I have absolutely zero idea why $pgArray[0] returns 0, yet var_dump($pgArray) returns array(5) { [0]=> string(7) .... That makes zero sense to me. Anybody know why $pgArray[0] is resolving to 0?
Wrapping your statement in parenthesis seems to work for me:
$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";
Without the parenthesis, it seems that the value breaks completely; the page doesn't print the <div> tags at all, but rather just adds a trailing 0 to the already printed 1.
I would assume that it's due to how PHP processes string concatenation, though I wouldn't be able to give you an exact answer as to why this happens. Just to be safe, I'd always either store any equations in variables before you pass them in, or perform all operations inside of parenthesis.
That way, you won't have weird encounters like this (for example):
echo "<div>" . 1 + 1 . "</div>"; // returns 1
echo "<div>" . (1 + 1) . "</div>"; // returns 2
You need to group the arithmetic part as mentioned in the accepted answer:
$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";
To understand what's going on here, you need to run it on the command line:
php > $rowCount = 5;
php >
php > $pgCount = ceil($rowCount / 10);
php >
php > $pgParamArray["page"] = $pgCount;
php >
php > $pgArray = array("", "", "", "", "");
php >
php > for ($i = 0; $i < 5; $i++) {
php {
php { if ($pgParamArray["page"] - $i > 0) {
php {
php { echo $pgParamArray["page"] - $i;
php {
php { $pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
php {
php { echo $pgArray[$i];
php {
php { }
php {
php { }
10</div>
php >
As you can see, the output of echo $pgParamArray["page"] - $i; is 1, immediately followed by 0</div> as the contents of $pgArray[$i].
So the real issue is, what is happening to the <div> in
$pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
After seeing the real output, the answer is a little more obvious now: it's a grouping issue. PHP is simply taking it left to right:
((("<div>". $pgParamArray["page"]) - $i) . "</div>")
((("<div>" . 1 ) - $i) . "</div>")
((("<div>1") - $i ) . "</div>")
((( 0 ) - $i ) . "</div>")
((( 0 ) - 0 ) . "</div>")
((( 0 )) . "</div>>")
((( "0</div>" )))
I am just learning coding, and couldn't find solution for that:
I'm creating a website that shows pictures that are on its server.
$number = $_POST["NR"];
$picture1 = "{$number}.jpg";
echo '<img src="pictures/' . $picture1 . ' ">';
How can I make it to repeat the echo string, but every time add for "$picture1" calculation +1 ? So, that the maximum count would be 40?
Thanks for helping. I know that it is actually easy, but I still don't know how to do it :)
Assuming that you have images named correctly awaiting this to happen:
$number = $_POST["NR"];
$i = 0;
while ($i < 40) {
$picture = $number + $i.".jpg";
echo '<img src="pictures/' . $picture . ' ">';
$i++;
}
Use a counter and loop. The issue here is that your $number could be anything... For instance if someone passed 500, you would need images named, 500.jpg, 501.jpg, etc. all the way up to 540.jpg. Unless I misunderstood the question.
I'm working with a multi-language site, and there is an HTML select element with 100 choices. The code looks like this:
<option value="1"><? echo $lang['CARGOTYPE_1']; ?></option>
So system insert into mysql table "type" number "1", or number "2" pr...
But when I select number from mysql, I need to change number to word.
With if, else I can change its meanings:
If($type == 1) { echo $lang['CARGOTYPE_1']; } elseif($type == 2){ echo $lang...
But the problem is, that code will be very long...
Any smart solutions for my problem?
If you know number of choices, use for cycle for that.
for ($i = 1; $i <= 30; $i++) {
echo '<option value="' . $i . '">' . $lang['CARGOTYPE_' . $i] . '</option>';
}
I am not sure what you want... If you want to "echo" something with arguments, you can do something like :
echo $lang['CARGOTYPE_'.$type];
But, is that you need ?
I have a loop that has a dynamic variable in it, eg:
while(i < 10){
echo ${"dynamic" . $i . "var"};
$i++;
};
I want to only echo the variable if the original var (say $dynamic3var) is set so I add:
while(i < 10){
if(isset(${"dynamic" . $i . "var"})){
echo ${"dynamic" . $i . "var"};
$i++;
};
};
However this wont work as its still picking up $i.
Does anyone know a correct way of doing this?
Since global variables are bad ideas you should rethink your code. A plain refactoring would be to use an associative array (even if it remains a global variable at the first step). Then you could work with
if( isset($dynamic[$i]) ) ...
Why are Globals evil? Read this: http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it
Try this:
while($i < 10){
$label = "dynamic".$i."var";
if(isset($$label))
echo $$label;
$i ++;
};
I'm trying to modify a joomla module and I have a small problem (which is not joomla related, thus no code necessary).
I have a foreach loop which has a block of code in it which displays an article. It repeats itself as many times as you set it up in the admin panel. I want to add the feature that makes this module display items on more than 1 column. All I need is the perk, I think I have everything else covered.
Basically how do I modify a simple foreach loop so that it displays articles on more than one column?
Instead of this
a
b
c
d
e
I want this
a ........ d
b ........ e
c
You can get the count of results and work from the middle if you're sticking with tables
$half_count = floor(count($entries) / 2);
for($i=0;$i<$half_count;$i++)
{
echo '<tr>';
echo '<td>' . $entries[$i] . '</td>';
echo '<td>' . (isset($entries[$half_count + $i]) ? $entries[$half_count + $i]: '') . '</td>';
echo '</tr>';
}
Here a simple way to do it :
php > $arr = array(1,2, 3, 4, 5, 6, 7, 8, 9, 10);
php > for ($i=0; $i<count($arr); $i+=2) { print $arr[$i] . "\t" . $arr[$i+1] . "\n"; }
1 2
3 4
5 6
7 8
9 10
As far as I know, you won't be able to do it with a foreach statement but with a for.
For example:
$iterations = (count($array_of_items) % 2) ? (count($array_of_items) / 2) + 1 : count($array_of_items) / 2;
for ($i = 0; $i <= $iterations; $i++) {
if (isset($array_of_items[$i+3]))
echo $array_of_items[$i].'........'.$array_of_items[$i+3];
else
echo $array_of_items[$i];
}
Really simple code without so little info but could make the trick!
Instead of outputting in the for loop, I would create two arrays of the articles in the for loop. Then loop through those arrays to create your columns outside of the main loop.
You have little info in the question, but here's something you could use
$arr = array("a","b","c","d","e","f");
for ($i = 0; $i<count($arr); $i++){
echo $arr[$i]." ". $arr[$i+3] ."\n";
if($i == 2){ break;} //Modify 2 as more alphabets are added
}
Outputs
a d
b e
c f
For a generic solution, assuming a packed array....
function show_table($data, $columns)
{
$items=count($data);
$iters=$items/$columns + ($items % $columns) ? 1 : 0;
for ($y=0; $y<$iters; $y++) {
for ($x=0; $x<$columns; $x++) {
$offset=$y*$columns + $x;
if ($offset<$items) print $data[$offset] . ' ';
}
print "\n";
}
}