How can I write the code for this in PHP language?
*
**
***
I want to print this stars shape in php, but the condition is by using single for loop. How is this possible?
My code is this where I am confused here:
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo "*".$a."<br/>";
}
echo "<br/>";
?>
You could use str_repeat() inside your loop like this
$a="*";
for($b=1;$b<=3;$b++)
{
echo str_repeat($a,$b).'<br>';
}
Result
*
**
***
To have this output * ** ***, you're may be looking for str_repeat
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo str_repeat($a, $b)."<br/>";
}
echo "<br/>";
And the solution without any builtin function would be the following:
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo $a."<br/>";
$a .='*';
}
echo "<br/>";
Related
I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>
When the page display results, I want it to say say "We found X products" or "We found 1 Product" rather than "we found 1 products".
The relevant part of my current code is this:
<?php echo sizeof($ids); ?> Products</strong>
How could I extend it to behave more natural?
If count is more than one echo "s".
<?php echo sizeof($ids); ?> Product <?php If(count($ids)>1) echo "s"; ?></strong>
Or you make it easier to read like this:.
If(count($ids)>1) {
Echo sizeof($ids) . " Products.</strong>";
}Else{
Echo sizeof($ids) . " Product.</strong>";
}
Hi I have made script that shows only even numbers and now i have to do that shows only 5 even numbers per row example:
numbers must be sorted like this
2,4,6,8,10
12,14,16,18,20
i must have numbers written like that current code that i am using for showing only even numbers is under. but if someone can help me how can i show only 5 per row i will be thankful. Thanks in advance.
<?php
$p=100;
for($p=100;$p>=0;)
{
echo "$p,";
$p=$p-2;
}
?>
Try something like this
<?PHP
$p=100;
$count=0;
for($p=100;$p>=0;)
{
echo "$p";
$p=$p-2;
$count+=1;
if($count==5){
$count=0;
echo "<br/>";
}
else echo " ,";
}
?>
One-liner:
echo implode("\n",array_map(function($a) {return implode(",",$a);},array_chunk(range(0,100,2),5)));
Result:
0,2,4,6,8
10,12,14,16,18
20,22,24,26,28
30,32,34,36,38
40,42,44,46,48
50,52,54,56,58
60,62,64,66,68
70,72,74,76,78
80,82,84,86,88
90,92,94,96,98
100
Function reference: implode, array_map, array_chunk, range.
For rendering in a browser, replace \n with <br />.
try the following
<?php for($p=100;$p>=0;$p-2){
echo "$p";
if($p%5==0){
echo "<br>";
}
else {
echo ", ";
}
}
?>
I am trying to display a list of friends using PHP and SQL, and my code partially works. However, it is returning the same result on multiple occasions and I would like it not to.
The SQL:
$sql =
"SELECT ubuser.usr_firstname, ubuser.usr_lastname, ubuser.usr_DOB,
ubuser2_1.usr_firstname & \" \" & ubuser2_1.usr_lastname
AS UBFriend, ubFriendsLink.ub_lnkID1, ubFriendsLink.ub_lnkID2, ubuser.usr_ID,
ubuser2_1.usr_ID
FROM ubuser
AS ubuser2_1
INNER JOIN (ubFriendsLink INNER JOIN ubuser ON ubFriendsLink.ub_lnkID1 = ubuser.usr_ID)
ON ubuser2_1.usr_ID = ubFriendsLink.ub_lnkID2
WHERE (((ubFriendsLink.ub_lnkID1) = ".$_SESSION['usr_ID'] ."))
OR (((ubFriendsLink.ub_lnkID2) = ".$_SESSION['usr_ID'] ."))";
The SQL works (or seems to).
The code for displaying the result:
<?php
$nrecs=0;
while (!$FriendsRs->EOF) {
$nrecs++;
?>
<? php
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
{
echo <p>Name: <?php echo $FriendsRs->Fields['UBFriend']->Value ?><br/ >
<? php
else
echo <p>Name: <?php echo $FriendsRs->Fields['usr_firstname']->Value ?> <?php
echo $FriendsRs->Fields['usr_lastname']->Value ?><br />
<?php $FriendsRs->MoveNext() ?>
<?php } ?>
The result:
Name: Carl Smith
Name: Rob Sanderson
Name: Rob Sanderson
Name: Tony Jackson
The problem seems to be that what I am getting is a list of the names associated with the ub_lnkIDs, where i only want to display the individual names. (FYI, Rob Sanderson is not needed, but it can be returned once).
EDIT:
The desired output is
Name: Carl Smith
Name: Tony Jackson
From what you've described it sounds like you want to remove the duplicates from your list.
I'm not sure what you're trying to do with the session check but your original if/else condition didn't make sense so I changed it to what might be right based on your query.
<?php
$Arrnames = array();
// Produces array containing all the names in the correct format
while (!$FriendsRs->EOF)
{
if ($SESSION['usr_ID'] == $FriendsRs->Fields['ub_lnkID1'])
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['UBFriend']->Value."<br />");
}
else
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['usr_firstname']->Value ." ". $FriendsRs->Fields['usr_lastname']->Value . "<br />");
}
$FriendsRs->MoveNext();
}
// Removes the duplicates from the array
$ArrnamesDedupped = array_unique($Arrnames);
// Loops the de-duplicated array and echos the result
foreach ($ArrnamesDedupped as $value)
{
echo $value;
}
?>
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
My best guess is the single = is causing your if statement to fail
== means is the same as (and can return false) WHEREAS = will always return true! (= is used for setting values.)
Additionally you open <?php tags above the if statement - and then open them again in your <?php echo without closing them?
<? php
if (.$SESSION['usr_ID'] = ['ub_lnkID1'])
{
echo <p>Name: <?php echo //HERE
There are multiple problems.e.g. opening
php tags inside php tags, no closing } for the if clause, html paragraphs (p) opened and not closed etc.
what I think you want to do (not tested):
<?php
$nrecs = 0;
while (!$FriendsRs->EOF) {
$nrecs++;
if ($SESSION['usr_ID'] !='ub_lnkID1') {
echo "<p>Name:" . $FriendsRs->Fields['usr_firstname']->Value . " " . $FriendsRs->Fields['usr_lastname']->Value . "</p>";
}
$FriendsRs->MoveNext();
}
?>
What I am trying to do is get an echo of the following php call and subtract 14.1% from the displayed number.
The code:
<?php echo $program->current_amount(); ?>
Can I add arithmetic functions to this in order to display the 14.1% deduction?
I think you're looking for a basic math operation in your output that has no effect on a database or anything else, correct?
If so, do something like the following:
<?php
// Set values
$current_amount = 100;
$pcnt_off = 14.1;
// Do the math
$out = $current_amount - ($pcnt_off/100) * $current_amount;
// Output
echo $out . " is " . $pcnt_off . "% off of " . $current_amount;
?>
http://codepad.org/RqF8cuvN
More specifically to your case:
<?php echo $program->current_amount() - 0.141 * $program->current_amount(); ?>
You can perform expressions inside an echo statement, yes; just wrap it in a (), so:
<?php echo ($program->current_amount() - .141); ?>
It may not even be necessary to use (). Incidentally, if your environment supports short tags, you can simply do:
<?= $program->current_amount() - .141 ?>
Keep in mind, though, that that code won't actually remove 14.1% from your number--you would want to multiply by .859.