Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed last month.
Improve this question
With this code:
$images = array(
'0.jpg',
'1.jpg',
'2.jpg'
);
$img_w_0 = 312;
$img_h_0 = 246;
$img_w_1 = 485;
$img_h_1 = 442;
$img_w_2 = 380;
$img_h_2 = 289;
foreach ($images as $i=>$image):
echo ' Width = '. ${'img_w_'.$image[$i]};
echo ' Height = '. ${'img_h_'.$image[$i]};
endforeach;
only the first iteration of my loop is outputting values. This is what I get:
Width = 312 Height = 246 Width = Height = Width = Height =
What I want to get is
Width = 312 Height = 246 Width = 485 Height = 442 Width = 380 Height = 289
I've created a Codepad to illustrate better.
What am I missing?
Try this
foreach ($images as $i=>$image):
// var_dump(substr($image, 0, 1));
echo ' Width = '. ${'img_w_'.substr($image, 0, 1)};
echo ' Height = '. ${'img_h_'.substr($image, 0, 1)};
endforeach;
You want to append 0, 1 and 2 but you was appending 0, ., j, p, g.
According with comments:
substr($image, 0, 1)
is equal
$i
but your code was iterating over letters in names of images.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
so I want to add two values (money values) but can't quite work it out.
I keep getting a notice, I've tried number_format to format it but even using that I get the 'Notice: A non well formed numeric value encountered'
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
$price = $price + $dbprice;
//echo number_format($price + $dbprice,2);
}
however I get this problem: Notice: A non well formed numeric value encountered
If I add two values '5' + '1.5' I get '6' as a result.
Edit:
The values I were pulling from the database were formatted with commas.
PHP numbers uses dot as separator and seems that you have coma. Please replace the coma with the dot as first. After that you can cast the prices into floats and finally sum it up :)
You can try with this (based on you example):
<?php
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
// Replace comma. Check also for possible thousand separator.
$dbprice = str_replace(',', '.', $dbprice);
$price = $price + (float)$dbprice;
//echo number_format($price + $dbprice, 2);
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for a quick way to highlight a text string if it has a flag i.e.(-R) as a first symbol in it, from the beginning till the line end (\n).
The function will be given a text of a multiple strings separated with "\n" that may or may not contain any flags at all. Something like this:
-Rnotes to be red
-Ynotes to be yellow
-G notes to be green
In the output of this function I need to get this:
<span style="background-color:red">notes to be red</span>
<span style="background-color:yellow">notes to be yellow</span>
<span style="background-color:green"> notes to be green</span>
Based on Matt's answer I made this answer.
Keep in mind it's Matt's idea it's based from so don't forget to upvote his answer if you like this.
I use str_replace to replace -R (or Y or G) with the span tag and use the -R as the key in color array.
Then I just add closing span tag.
$notes = array(
'-Rnote',
'-Gnote',
'-Ynote',
'-Rnote1',
'-Gnote1',
'-Ynote1',
);
// Above array can be replaced by:
// $notes = explode("\n", $text);
$colors = array(
'-R' => 'red',
'-G' => 'green',
'-Y' => 'yellow',
);
foreach ($notes as $note ) {
If (isset($colors[substr($note,0,2)])){
echo str_replace(substr($note, 0, 2), '<span style="background-color:' . $colors[substr($note, 0, 2)] . '">', $note) . "</span>\n";
}Else{
Echo $note ."\n";
}
}
https://3v4l.org/9YDAX
Edit noticed how I forgot that there may not be a color tag in the string.
Added a echo of no color notes
You could try something link this:
$notes = array(
'-Rnote',
'-Gnote',
'-Ynote',
'-Rnote1',
'-Gnote1',
'-Ynote1',
);
$colors = array(
'R' => 'red',
'G' => 'green',
'Y' => 'yellow',
);
foreach ($notes as $note ) {
$color_key = substr($note, 1, 1);
$note_string = substr($note, 2);
$bg_color = array_key_exists($color_key, $colors) ? $colors[$color_key] : '';
echo '<p style="background-color:' . $bg_color . '">' . $note_string . '</p>';
}
Storing your colors in an associative array may make it easy to add more colors in the future.
This might be a little messy, but it should work for you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
Need solution or description of: How to convert color value from ARGB to RGBA which is possible to use for css:
Examples:
ARGB color : #ff502797 convert to RGBA
RGBA result example: rgba(80,39,151,1)
thx.
ARGB contains 4 groups of HEX values(channels): Alpha, Red, Green, Blue.
Scheme:
Exampe: #ff502797 - color in ARGB formatt
Elements on positions:
0,1 - Alpa (ff)
2,3 - Red (50)
4,5 - Green (27)
6,7 - Blue (97)
After this convert each channel to decimal. And putt on their places into RBGA:
rgba(Red,Green,Blue,Alpha) - rgba(80,39,151,1)
Example function:
function argb2rgba($color)
{
$output = 'rgba(0,0,0,1)';
if (empty($color))
return $output;
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 8) { //ARGB
$opacity = round(hexdec($color[0].$color[1]) / 255, 2);
$hex = array($color[2].$color[3], $color[4].$color[5], $color[6].$color[7]);
$rgb = array_map('hexdec', $hex);
$output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')';
}
return $output;
}
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 problem here we I would like to add on the integer part of a serial number. Say I read as JK001256. Thus I can use substring to remove the first 2 char and the rest 6 as numerics. The problem I want to add to the number say 50 means it starts from JK001256 to JK0012306. If I extra the numeric it get 1256 and lose the 00. Any ideas?
$stringVersionWithLeadingZeroes = sprintf('%06d', $numericPart)
http://php.net/manual/function.sprintf.php
Update
In order to provide something complete, try this
if (!preg_match('/^([A-Z]{2})(\d{6})$/', $serial, $parts)) {
throw new Exception('Invalid serial number');
}
$newSerial = sprintf('%s%06d', $parts[1], (int) $parts[2] + 50);
$serial = 'JK001256';
sscanf($serial, 'JK%d', $serial_no);
$new_serial = sprintf('JK%06d', $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Working demo: http://codepad.org/jbBnofUh
Any 2-chars prefix:
$serial = 'JK001256';
sscanf($serial, '%2s%6d', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%06d', $serial_prefix, $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/aE0Htgyh
Any non-digit prefix:
$serial = 'JK001256';
sscanf($serial, '%[^0123456789]%6d', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%06d', $serial_prefix, $serial_no + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/aQksxJjU
Any non-digit prefix & unspecified digits length:
$serial = 'JK001256';
sscanf($serial, '%[^0123456789]%s', $serial_prefix, $serial_no);
$new_serial = sprintf('%s%0' . strlen($serial_no) . 'd', $serial_prefix, intval($serial_no) + 50);
var_dump($new_serial); // string(8) "JK001306"
Demo: http://codepad.org/fyYskTWP
$last_part = substr("JK001256", -2); // 56
$first_part = substr("JK001256",0, -2); // JK0012
$sum = $last_part + 50;
$new_string = $first_part.$sum; // JK0012306;
I dont know if its the best way to do it, but it should work
You can use str_pad:
<?php
$value1 = substr("JK001256",2, 6);
$first = substr("JK001256",0, 2);
$index = 50;
for($i=0;$i<=$index;$i++)
{
$value2 = $value1 + $i;
$new_index[] = $first."".str_pad($value2, 6, "0", STR_PAD_LEFT);
}
print_r($new_index);
?>
http://codepad.org/CAlJRkJ4
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm writing code to show the prices of some products. If the price is 0 it doesn't show anything.
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
<? if ($book->price_gbp != 0) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if ($book->price_usd != 0) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>
This echoes "€0.00 £33.00 $66.00 ". The € price is set to 99. I can see no reason whatsoever that this should echo as 0! Am I doing something wrong? Bad syntax?
$europrice = number_format($book->price_eur, 2
shouldn't it be $book->price_euro ?
Typo in Euro
number_format($book->price_eur, 2); // This is what you have.
number_format($book->price_euro, 2); // This is what you need.
I don't see anything wrong with this code without seeing more. But maybe your variable?
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
price_eur should be price_euro?
number_format($book->price_eur, 2);
should be
number_format($book->price_euro, 2);
The null value is causing your problem.
You might still be getting null values or something else try using empty instead
<? if (empty($book->price_euro)) {$europrice = number_format($book->price_euro, 2); echo "€$europrice";}?>
<? if (empty($book->price_gbp)) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if (empty($book->price_usd)) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>