How to concatenate between two variables? - php

I need to include the - to my database whenever I select the year, but only, 2006 2008 are saving in my database. How is this guys?
('<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2.' - '.$i.'</option>'."\n");

Put it in the value attribute, like this:
('<option value="'.$year2. " - " . $i .'" '.$selected.'> '.$year2.' - '.$i.'</option>'."\n");

Then you should put the - in the value attribute and not just the label text.

use this
'<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2." - ".$i.'</option>'."\n";

Related

Convert SQL query with COALESCE and LEFT JOIN to PHP [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
Why does the following code output 0?
It works with numbers instead of strings just fine. I have similar code in JavaScript that also works. Does PHP not like += with strings?
<?php
$selectBox = '<select name="number">';
for ($i=1; $i<=100; $i++)
{
$selectBox += '<option value="' . $i . '">' . $i . '</option>';
}
$selectBox += '</select>';
echo $selectBox;
?>
This is because PHP uses the period character . for string concatenation, not the plus character +. Therefore to append to a string you want to use the .= operator:
for ($i=1;$i<=100;$i++)
{
$selectBox .= '<option value="' . $i . '">' . $i . '</option>';
}
$selectBox .= '</select>';
In PHP use .= to append strings, and not +=.
Why does this output 0? [...] Does PHP not like += with strings?
+= is an arithmetic operator to add a number to another number. Using that operator with strings leads to an automatic type conversion. In the OP's case the strings have been converted to integers of the value 0.
More about operators in PHP:
Reference - What does this symbol mean in PHP?
PHP Manual – Operators
PHP syntax is little different in case of concatenation from JavaScript.
Instead of (+) plus a (.) period is used for string concatenation.
<?php
$selectBox = '<select name="number">';
for ($i=1;$i<=100;$i++)
{
$selectBox += '<option value="' . $i . '">' . $i . '</option>'; // <-- (Wrong) Replace + with .
$selectBox .= '<option value="' . $i . '">' . $i . '</option>'; // <-- (Correct) Here + is replaced .
}
$selectBox += '</select>'; // <-- (Wrong) Replace + with .
$selectBox .= '</select>'; // <-- (Correct) Here + is replaced .
echo $selectBox;
?>

How to create html table in for loop of php script?

I would like to create table for the below for loop. It prints the square of numbers from 1 to 10. I want to declare a html table and want to show the output like this
"1 square is" one cell, "1" in second cell
"2 square is" in one cell, "4" in second cell
<?php
for ($i=1;$i<=10;$i++)
echo $i . ' square is ' . $i*$i . "<br>";
?>
Thanks alot.
<?php
echo '<table>';
for ($i=1;$i<=10;$i++)
echo '<tr><td>' . $i . ' square is </td><td>' . $i*$i . "</td></tr>";
echo '</table>';
?>

PHP compare 2 arrays from database and checkbox=checked if any of the values are found

I have a column(apps) in the database which has a list of words separated with commas like - AAA, BBB, CCC
I have another column(appsSelected) that will be populated in the same format with the words that the user checks with check boxes values.
On one page i list all of the words in apps with check boxes beside them, on submit it inserts those checked into appsSelected.
On another page it lists only the words in the appsSelected column, I have managed to grab appsSelected and explode it to list each word.
<?php
$appsString = $pi_row['appsSelected'];
// break $appsString using the comma as the delimiter
$appliances = explode(', ', $appsString);
// loop through and print all the words
echo '<ul>';
for ($i = 0; $i < count($appliances); $i++)
{
echo '<li>' . $appliances[$i] . '</li><br/>';
}
echo '</ul>';
?>
The problem i have is when i go back to the page with check boxes, i want them to stay checked on the words that are in appsSelected.
So im guessing i have to compare apps with appSelected and echo out either a checked box or un-checked depending on what words match.
for ($i = 0; $i < count($appliances); $i++)
{
$codes = array($pi_row['apps']);
$codesSelected = array($pi_row['appsSelected']);
//if (in_array($appliances[$i], $codesSelected)) {
if (array_intersect($ap, $aps)) {
echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
} else{
echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
}
}
The code above only works if appsSelected matches the entire array in apps:
for example in the apps column - AAA, BBB, CCC
if i tick AAA and BBB or any other combonation that doesnt involve matching every word in apps no check boxes get ticked but if i tick AAA, BBB, CCC the check boxes will return all ticked
I need to find a way to see if any word in apps matches any word in appSelected.
Use in_array like you did in your comment, you have to pass it the correct array though:
$codesSelected = explode(', ', $pi_row['appsSelected']);
for ($i = 0; $i < count($appliances); $i++)
{
if (in_array($appliances[$i], $codesSelected)) {
echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
} else {
echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
}
}

MySQL/PHP mysql_fetch_array() keeps missing first row

Good eve everyone!
For some reason Database::fetchArray() is skipping the first $row of the query result set.
It prints all rows properly, only keeps missing out the first one for some reason, I assume there's something wrong with my fetchArray() function?
I ran the query in phpMyAdmin and it returned 4 rows, when I tried it on my localhost with the php file (code below) it only printed 3 rows, using the same 'WHERE tunes.riddim'-value ofcourse. Most similiar topics on google show that a common mistake is to use mysql_fetch_array() before the while(), which sets the pointer ahead and causes the missing of the first row, unfortunately I only have one mysql_fetch_array() call (the one within the while()-head).
<?php
$db->query("SELECT " .
"riddims.riddim AS riddim, " .
"riddims.image AS image, " .
"riddims.genre AS genre, " .
"tunes.label AS label, " .
"tunes.artist AS artist, " .
"tunes.tune AS tune, " .
"tunes.year AS year," .
"tunes.producer AS producer " .
"FROM tunes " .
"INNER JOIN riddims ON tunes.riddim = riddims.riddim " .
"WHERE tunes.riddim = '" . mysql_real_escape_string(String::plus2ws($_GET['riddim'])) . "'" .
"ORDER BY tunes.year ASC");
$ar = $db->fetchArray();
for($i = 0; $i < count($ar) - 1; $i++)
{
echo $ar[$i]['riddim'] . " - " . $ar[$i]['artist'] . " - " . $ar[$i]['tune'] . " - " . $ar[$i]['label'] . " - " . $ar[$i]['year'] . "<br>";
}
?>
Database::fetchArray() looks like:
public function fetchArray()
{
$ar = array();
while(($row = mysql_fetch_array($this->result)) != NULL)
$ar[] = $row;
return $ar;
}
Any suggestions appreciated!
You should remove -1 from the for loop
The problem's in your while loop:
for($i = 0; $i < count($ar) - 1; $i++)
if count ($ar) is 1, because there's one entry, your loop will never be called; try tweaking the check part:
for($i = 0; $i < count($ar) ; $i++)
You can also use a simple foreach:
foreach($db->fetchArray() as $row)
{
echo $row['riddim'] # ...
}
It'll make your code more readable too.

How to round mysql column in a table output

I need to round a result of a SQL round in a table array output. I can't figure out the syntax...
$result = mysql_query("SELECT `Energ_Kcal`*`yield`*`qty` AS `cal` FROM allinnot a
WHERE `own_id` = $user->id");
echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td> " . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";
cal returns a value with many numerals beyond the decimal. I just need it to show a whole rounded integer. I tried ROUND(), but I must be putting it in the wrong place.
general syntax
ROUND( expression, 2 )
The ROUND should go around the values, try this:
$result = mysql_query("SELECT ROUND(`Energ_Kcal`*`yield`*`qty`,2) AS `cal` FROM allinnot a
WHERE `own_id` = $user->id");
echo "<tr><td>" . $row['Shrt_Desc'] . "</td><td> " . $row['desc'] . "</td><td>" . $row['cal'] . " cal</td></tr>";
You could also check out PHP's round() or, possibly from your description int_val().

Categories