I want to make some selection and read from mysql database in php.
I create this query:
$sql0="SELECT descrizioni_marche.Id_marca, descrizioni_marche.Id_categoria,
marche.ID as id_man,marche.marca as maname ,GROUP_CONCAT(categorie.categoria) as cat,categorie.ID as id_cat
FROM marche,descrizioni_marche,categorie
WHERE descrizioni_marche.Id_marca=marche.ID
AND descrizioni_marche.Id_categoria=categorie.ID
GROUP BY maname
";
I want to output some of the categories named "cat" like below:
<b>' . $row0["cat"] ."". '</b>
But because I have used GROUP_CONCAT all the $row0["cat"] display in one row separated by comma and included in one link. I want them to be in different rows and each one of them to have a different link. Is there a way to do this?
Thanks!
After I do this according to a comment below:
echo ' <b>' . explode(",", $row0['cat']) ."". '</b>';
The result is:Array to string conversion in
To print all link at same place
array_walk((explode(',',$row0['cat'])),function($category){
echo '' . $category .'';
});
Related
I am a bit stuck I am attempting to build out a table with HTML and PHP. Ok got that done. Now I want to add a dynamic dropdown as one of the column options but cant seem to figure out how to mix the PHP, the HTML and the needed FOR EACH loop.
I know its currently wrong but am posting a variable of what I have been trying below.
echo '<td>' . "<select>". "<option value =" . $sf_name . ' '. $sl_name . ">" . $sf_name . ' '. $sl_name . "</option>" .
foreach($Staff_On_Duty as $person){
"<option value =" . $sf_name_option=$person->Staff_First_Name . ' '. $sl_name_option=$person->Staff_Last_Name . ">" . $sf_name_option . ' '. $sl_name_option . "</option>"
}
. "</select>" .'</td>';
I need to have the currently selected individual at the top of the dropdown with the option to change that person out. the first .sf_name comes from higher in the code and gives me the name of the individual currently selected. The foreach runs from the $Staff_On_Duty query to give me everyone working right now. What is the right way to do this?
This is how I would do it, essentially only fill the dropdown and mark the option that should have been selected (other stuff is a matter of preference):
echo("<td><select>");
foreach($Staff_On_Duty as $person){
$sf_name_option=$person->Staff_First_Name; // separated for clarity, you could also use $person->Staff_First_Name everywhere
$sl_name_option=$person->Staff_Last_Name;
echo("<option value = $sf_name_option $sl_name_option");
if (($sf_name_option == $sf_name) && ($sl_name_option == $sl_name)) echo (" selected"); // this is the relevant part, but make sure variable values match!
echo(">$sf_name_option $sl_name_option</option>");
}
echo("</select></td>");
Note that I did not put any quotation marks cause I have no idea what your variables entail, but it would need them if you don't have them in yet, i.e.:
... value=\"$sf_name_option $sl_name_option\" ...
I'm trying to create a page that displays content from database when specific title is selected, and all the comments added to it (only comments for the selected title should be visible) by other users. I want make the id and review_id to work together on the same line. I cant create two separate echo's as it creates two separate titles.
I've tried to link the values together but cant make it work.
echo '<li>' . $comment['name'] . ' - ' . $comment['date'] . '</li>';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
echo '<li>' . $comment['name'] . ' - ' . $comment['date'] . '</li>';
Is there a way to make "id" and "review_id" work at the same line ?
Being a beginner in PHP, I have a table in a database which consists of 14 columns. I need to extract some of the columns through an 'href' link on another page. I am having a hard time trying to get the specific column ids for the specific column as I need the columns to be displayed as plain text separately on an html page.
So this is the fetch code to display columns 'A' and 'G' including two links in a table.
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['A'] . "</td>";
echo "<td>" . $row['G'] . "</td>";
echo "<td>FASTA</td>";
echo "<td>Full Entry</td>";
echo "</tr>";
}
I am facing problems to get the columns A to M separately on the next php page of FullEntry.php and I need the data from the columns as plain text.
This is what I could come up with on FullEntry.php page.
$row = $_GET['rowid'];
<!--Here is where I need the multiple row ID's to get separate columnar data-->
echo $row;
?>
So How can use different id's for different columns from the original.php page to display results separately through the FullEntry.php page or If there can be any modifications with sql query as well. Help will be appreciated.
Any help would be appreciated.
Thank you in advance!
HereI have added a delimiter | bewteen $row reults like
echo "<td>Full Entry</td>";
And the result will be like Fullentry.php?rowid=a|b|c|d|e..... and you can access this by exploding the rowid.
$result = $_POST['rowid];
$result = explode("|",$result);
echo $result [0];
echo $result [1];...
Maybe I'm not thinking this through correctly but I have a company table with:
name
address
city
state
zip
I want to search two fields, name and city.
My query is:
SELECT DISTINCT city, name
FROM companies
WHERE city
LIKE '%$search%'
OR name
LIKE '%$search%'
and the results code is:
echo '<a href="city.php?city=' . $results['city'] . '">'
. $results['city'] . '</a>';
Now this only takes into account if the person searches for a city. How do I get it to show the page if someone searches for a company name? Something like:
echo '<a href="company.php?co=' . $results['name'] . '">'
. $results['name'] . '</a>';
Is there any way to show the proper result depending on what they searched for?
If you have only one search box, how can you know what the user is looking for?
You could add checkboxes/radio buttons to offer the choice between city or name.
If you don't want to do that then you do not know what the user is searching for. You can however run two queries, one for city then another one for name. If you get results you will at least know where they are coming from.
Why don't you compare the two fields of the results to the search text, to know what matched it: city or name.
Something like:
if($search === $results['city']) echo ''. $results['city'] . ''; elseif($search === $results['name'])
echo ''. $results['name'] . '';
Not sure if I understood right...
I am using google charts API to plot some data. I am grabbing the data from a MySQL database and printing it using MySQL.
<?php
$income_from_clients = $mysql_obj->select ( "
SELECT
sum(invoice.invoice_amount_payable) AS invoice_totals,
company.company_label AS company_label,
MONTHNAME(invoice.invoice_date_paid) AS month,
YEAR(invoice.invoice_date_paid) AS year
FROM invoice INNER JOIN company ON invoice.company_id = company.company_id
WHERE
invoice.invoice_active=1
AND invoice.invoice_status_id=7
AND invoice.invoice_date_deleted=0
AND invoice.invoice_date_paid>='" . $_POST ["start_date_"] . "-" . $_POST ["start_date_month"] . "-" . $_POST ["start_date_date"] . "'
AND invoice.invoice_date_paid<='" . $_POST ["end_date_"] . "-" . $_POST ["end_date_month"] . "-" . $_POST ["end_date_date"] . "'
AND company.company_id=" . $_POST ["company_id"] . "
GROUP BY company.company_id, MONTH(invoice.invoice_date_paid), YEAR(invoice.invoice_date_paid)
ORDER BY YEAR(invoice.invoice_date_paid), MONTH(invoice.invoice_date_paid)" );
echo "data.addRows($mysql_obj->mysql_num_rows);";
$i = 0;
foreach ( $income_from_clients as $ifc ) {
echo "data.setValue($i, 0, '" . $ifc ["month"] . " " . $ifc ["year"] . "');\n";
echo "data.setValue($i, 1, " . $ifc ["invoice_totals"] . ");\n";
$i ++;
}
?>
This is working fine however any months where there has not been an invoice is simply skipped on the chart as it isn't returned returned from the MySQL. How would I get it to plot a 0 for these months?
Thanks.
To fill holes in the data in MySQL, you need to join it with a reference table (calender in your case).
See - Using a Join to Fill in Holes in a List
At the moment, you're iterating over the results from the database and outputting values based on the rows it returns. Since it doesn't return the rows with a value of 0, you won't see those in your output.
Two viable solutions include:
Modify your SQL so that it returns the rows with a value of 0 (see Sukumar's answer)
Change your PHP so that it iterates over the year/month combinations you are interested in and outputs the value from the database or 0 if there isn't one. I'm not sure which ones you are "interested in", but you might be able to iterate FROM "smallest year/monthcombination returned from the db" TO "largest year/monthcombination returned from the db" (or current year/month). If you do this, you'll likely want to iterate through the results in the database and put them in a (year/month -> value) map... then iterate over the dateranges and pull values from the map.