Getting error from php what did I do wrong? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I get the following error.
[02-Nov-2015 16:29:48 America/Los_Angeles] PHP Warning:
number_format() expects parameter 1 to be double, string given in
C:\inetpub\wwwroot\handpaytest\index.php on line 17
This the code that its complaining about.
echo "<td>Past Week: $" . number_format(htmlspecialchars($cell)) . "</td>";
In context
<div id='Title'></div>
<div id='week'>
<?php
echo "<center><table>\n\n";
$f = fopen("week.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>Past Week: $" . number_format(htmlspecialchars($cell)) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</center></table>";
?>
</div>

What htmlspecialchars does is Convert special characters to HTML entities
So, you need to Type caste $cell as (float)$cell before passing it to
number_format
number_format((float)$cell)

As we don't know what $cell contains, You can try to force its type to be float.
echo "<td>Past Week: $" . number_format((float)$cell)) . "</td>";

Related

Print a new <tr> each time & "N" number of elements inside it, for a total quantity of "Q" [closed]

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 3 years ago.
Improve this question
i have a variable Q,
i want to print "cat" (or whatever) Q times in HTML table rows, only each row of the HTML table can contain maximum of 5 cats.
like if Q <= 5 then i want to print a single <tr>.
& if Q = 10;
then i want to print 2 rows each containing 5 cats.
can someone help me here?
I'm using php as the programming language and HTML.
thanks in advance.
You can also make use of array_chunk to decide the chunk(no. of times) for each row and repeat a string using str_repeat the chunk size times to achieve your goal.
<?php
$times = 12;
$limit_for_each_row = 5;
$string = "Cat";
echo "<table>";
foreach(array_chunk(range(1,$times),$limit_for_each_row) as $chunk){
echo "<tr>" . str_repeat("<td>$string</td>",count($chunk)) . "</tr>";
}
echo "</table>";
Demo: https://3v4l.org/EWgHh
May following code will help you to achieve requested output:
<?php
$print_label = "meow";
$q = 15;
$steps = 5;
echo "<table>";
for($i=1;$i<=ceil($q/$steps);$i++) :
echo "<tr>";
for($j=1;$j<=$steps;$j++) :
echo "<td>".$print_label."</td>";
endfor;
echo "</tr>";
endfor;
echo "</table>";
One way to solve this problem is by using array_fill to make an array of $q copies of the word; array_chunk to split that into the maximum number of words per row; and implode to put all the words in each chunk together into a row:
$word = "cat";
$q = 13;
$row_max = 5;
$words = array_fill(0, $q, $word);
$chunks = array_chunk($words, $row_max);
foreach ($chunks as $chunk) {
echo '<tr><td>' . implode('</td><td>', $chunk) . '</td></tr>' . "\n";
}
Output:
<tr><td>cat</td><td>cat</td><td>cat</td><td>cat</td><td>cat</td></tr>
<tr><td>cat</td><td>cat</td><td>cat</td><td>cat</td><td>cat</td></tr>
<tr><td>cat</td><td>cat</td><td>cat</td></tr>
Demo on 3v4l.org

How to divide CSV to two different Strings in PHP [closed]

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 4 years ago.
Improve this question
I am trying to divide CSV into Two String LHS_String and RHS_String. For Example, I have the below CSV -
customer^id,record^id
customer^fname,record^first_name
cusomer^gender,record^gender
The Output should be like -
LHS_String : customer^id
RHS_String : record^id
Short answer, read over http://php.net/manual/en/function.explode.php
Example:
<?php
$data = "customer^id,record^id\r\n";
$data .= "customer^fname,record^first_name\r\n";
$data .= "cusomer^gender,record^gender\r\n";
$p1 = explode("\r\n", $data);
$p2 = array();
foreach($p1 as $key => $line){
$p2[$key] = explode(",", $line);
}
echo "LHS_String : " . $p2[0][0] . "\r\n";
echo "RHS_String : " . $p2[0][1] . "\r\n";
?>

Can't Sort PHP Array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anybody tell me why I can't get this to work?
I am simply trying to sort my arrays from highest to lowest.
$stuff[] = "100";
$stuff[] = "104";
$stuff[] = "102";
$stuff[] = "103";
$stuff[] = "101";
$stuff[] = "99";
echo "Largest: " . max($stuff) . " <br> \n";
arsort($stuff);
echo "0 : " . $stuff[0] . " <br> \n";
echo "1 : " . $stuff[1] . " <br> \n";
echo "2 : " . $stuff[2] . " <br> \n";
echo "3 : " . $stuff[3] . " <br> \n";
echo "4 : " . $stuff[4] . " <br> \n";
echo "5 : " . $stuff[5] . " <br> \n";
arsort() is sorting your array, but it also conserve the keys of each value. Witch mean the key 0 will still answer with 100, and not the first element !
With a foreach, you would see them in correct order.
foreach ($stuff as $value) {
echo $value . "<br>\n";
}
OR
You can also use rsort() that will not be associative, which mean the keys won't have the same values after it.

Resolve PHP post string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following post string:
Name=John&Age=33&Question1=What's+your+gender%3F&Answer1=Male&Question2=What's
+your+education%3F&Answer2=Graduate
I'm going to have randomly generated amount of questions each time so I won't be able to know number of passed variables. Thus, I am searching for a solution something like:
foreach($postItem as $varr) {
echo $varr["name"].": ".$varr["value"]."<br />";
}
Post them as answer[1], answer[2] etc instead of answer1, answer2, and PHP will turn them into an array for you.
//parse query string to an array ($output)
parse_str($postString, $output);
echo $output['Name'] . ': ' . $output['Age'] . '<br />';
//process questions
$i = 1;
while (array_key_exists('Question' . $i, $output)) {
echo $output['Question' . $i] . ': ' . $output['Answer' . $i] . '<br />';
$i++;
}
function used http://php.net/parse_str

Please specify how to implement this code between echo ' '; [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";
<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'

Categories