I have a query like this
$sql = "SELECT SUM(CASE WHEN jr_softwarecheck LIKE \'%sony\' AND
jr_othersoftware LIKE \'%sony%\' THEN 2 ELSE 1 END) AS totalcount FROM
jos_jreviews_content WHERE jr_softwarecheck LIKE \'%sony%\' OR
jr_othersoftware LIKE \'%sony%\'";
I want to output results in HTML pages. I run a Joomla based site.
How can I do that?
Sorry but I'm not so skilled in PHP, I'm learning.
Expected result in HTML page (frontend), example:
SONY Products: 105
Thanks in advance to all!
In your case, use it like this:
$sql = "SELECT SUM(CASE WHEN jr_softwarecheck LIKE \'%sony\' AND jr_othersoftware LIKE \'%sony%\' THEN 2 ELSE 1 END) AS totalcount FROM jos_jreviews_content WHERE jr_softwarecheck LIKE \'%sony%\' OR jr_othersoftware LIKE \'%sony%\'";
$res = mysql_query($sql); // This will run the query on the connected datababse
if($row = mysql_fetch_array($res)){ // Since you are using just a SUM to count results, you don't need to loop
echo "Sony Products: ".$row['totalcount']; // $row['totalcount'] is the result of the totalcount from your MySQL query put into the $row variable
}
I hope this helps you out :)
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
//do something
}
Related
here's the code and i want to echo only 1 city from mysql database!
<?php
include('db.php');
$queryss = mysqli_query($conn, 'SELECT * FROM areas');
while ($rowx = mysqli_fetch_array($queryss)) {
echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";
}
?>
and i'm, getting this input here! 3 time karachi in my html, but i want only 1 of this city. SELECT DISTINCT is working in mysql but how can i use it in PHP?
Your query should be
SELECT * FROM areas GROUP BY id
I have tested it.
Use
SELECT DISTINCT column_name1,column_name2 FRAM areas
in your SQL where column_nameN stands for the columns you need for your output.
OR use something like this (untested):
$results = [];
while ($rowx = mysqli_fetch_array($queryss)) {
$results[] = $rowx;
}
$results= array_unique($results);
foreach($results as $rowx) {
echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";
}
First Solution
You can insert distinct keyword into your SQL to accomplish what you need, like so
SELECT DISTINCT your_column_name FROM table_name
Second Soltion
You can execute your SQL statement and then use array_unique, to be like so
$selectStatement = mysqli_query($con, 'SELECT * FROM areas');
$selectedArrayValues = mysqli_fetch_array($selectStatement);
$selectedUniqueArrayValues = array_unique(selectedArrayValues);
// Then return that array to your HTML code
I recommend the first solution because it's more optimized
I have a MySQL, PHP code as follows.
$sql = "SELECT * FROM shipschedule WHERE ship_date BETWEEN '2016-08-01' AND '2016-8-31'";
$result = $mysqli->query($sql);
$e = array();
while($r = $result->fetch_array()) {
$rows = array();
$rows['title'] = $r['title'];
$rows['start'] = $r['ship_date'];
array_push($e, $rows);
}
echo json_encode($e);
The above php code echos
[{"title":"111","start":"2016-08-10"},
{"title":"111","start":"2016-08-10"},
{"title":"111","start":"2016-08-10"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
My question is how I can echo the above as follow instead. Please see that duplicate start dates will be removed by title.
[{"title":"111","start":"2016-08-10"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
title 111 has the same 3 start dates, and I need to display it like
{"title":"111","start":"2016-08-10"},
title 222 has the same 2 start dates, and I need to display it like
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
You could prevent receiving duplicates, and reduce requesting unnecessary data by adjusting your query.
SELECT DISTINCT title, start FROM ...
It would be much easier (and probably faster too) to just get the right (unique) data from MySQL. This can be achieved with the distinct modifier:
SELECT DISTINCT title, start
FROM shipschedule
WHERE ship_date BETWEEN '2016-08-01' AND '2016-8-31'
I have a table called "guesses" that stores peoples guesses whether a baby is a boy or a girl. Those are the two possible things in the "sex" column (i.e. "boy" or "girl).
There are 4 guesses in the table for this poolid. So if I run this code below...
$sql = "SELECT
FROM guesses
WHERE poolid = '$poolid'
ORDER BY $sort, createddate";
$getguesses = mysqli_query($connection, $sql);
if (!$getguesses) {
die("Database query failed: " . mysqli_error($connection));
} else {
//Get total number of guesses
$numguesses=mysqli_num_rows($getguesses);
echo "NUMGUESSES: $numguesses";
while ($row = mysqli_fetch_array($getguesses)) {
//code to grab other info about guesses, not relevant, works fine
}
}
It outputs
NUMGUESSES: 4
And also spits out 4 lines (one for each guess) with other info that isn't relevant.
I would like to count the total number of girl guesses vs. boy guesses, for use later in a pie chart. So I did the following...
$sql = "SELECT *, COUNT(CASE WHEN `sex` = 'girl' then 1 ELSE NULL END) as 'totalgirls', COUNT(CASE WHEN `sex` = 'boy' then 1 ELSE NULL END) as 'totalboys'
FROM guesses
WHERE poolid = '$poolid'
ORDER BY $sort, createddate";
$getguesses = mysqli_query($connection, $sql);
if (!$getguesses) {
die("Database query failed: " . mysqli_error($connection));
} else {
//Get total number of guesses
$numguesses=mysqli_num_rows($getguesses);
echo "NUMGUESSES: $numguesses";
while ($row = mysqli_fetch_array($getguesses)) {
echo "GIRLS:". $row['totalgirls'];
echo "BOYS:". $row['totalboys'];
//code to grab other info about guesses, not relevant
}
}
This outputs
NUMGUESSES: 1
GIRLS: 4
BOYS: 0
And also spits out ONLY ONE line (for only one of the four existing guesses)
All four guesses are girls, so the GIRLS total and the BOYS total are correct. But why is it only seeing NUMGUESSES as 1 now? It should be 4 and should show 4 lines of guesses.
Something with the COUNT() is throwing something off. Any ideas?
You're asking mysql to count, so it outputs one line ; )
Try this instead:
$sql = "SELECT COUNT(*), SUM(CASE WHEN `sex` = 'girl' then 1 ELSE 0 END) as.....
Beware of including NULL values in a count!
I have an mysqli_query that pulls 3 columns and 10 rows from my table. I would to assign a variable to each row of the query. I could do 10 seperate querys and assign each to a variable, but i assumed that would be frowned upon. (note: connection info in seperate file and not shown here).
<?php
$playoffseedings = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed > 0
ORDER BY playoffseed
LIMIT 10");
$seedings = array($playoffseedings);
FOREACH($seedings as $ps){
$num_rows = 1;
while($row = mysqli_fetch_array($ps)){
$num_rows++;
echo $row['playoffseed'].$row['ttName'];
echo "<br>";
}}
?>
The FOREACH used above works fine and returns 10 rows of the data I queried. So that's cool. But I need to be able to display different rows in different places on my page. So I thought assigning each row to a different variable would work well.
I tried adding something such as this to assign a variable to each row so that I'd have 10 different variables $seed1, $seed2, $seed3..., each $seedx would be an array of playoffseed, ttName, and ttsUID.
FOREACH($seedings as $ps){
$num_rows = 1;
while($row = mysqli_fetch_array($ps)){
$num_rows++;
$seed$num_rows = $row[$num_rows];
}}
I'd like to know how to do the above, but also, I was confused on my journey as to why I couldn't echo a row from the an array using something like
echo $seedings[1];
echo $seedings[7]['playoffseed'];
Why wouldn't 'echo $seedings[1];' return the result of the second row of the $seedings array? I got a blank.
And why wouldn't 'echo $seedings[7]['playoffseed'];' return the playoffseed of the 8th row? Again, blank.
I felt the 2 questions were tied together because I thought I could either refer to the data I wanted to later with either a variable (ie. $seed3), or I could possibly refer to the data I need using the index (keys?) of the array (ie $seedings[7]['playoffseed']).
First post! Thanks for the help. Usually always find what I need after a few hours of searching. Stuck on this one after a few days of looking.
Edit (In an attempt to clarify):
My query returns a table (multidimensional array?) like this...
ttsUID ttName playoffseed
1993 Buffalo 1
1993 Miami 2
1993 Detroit 3
1993 Denver 4
1993 Chicago 5
...and so on. 10 rows total
After calling that query I'd like to set variables ($seedx) such as this...
$seed1 = (1993, Buffalo, 1)
$seed2 = (1993, Miami, 2)
$seed3 = (1993, Detroit, 3)
...and so on until $seed10
So each $seedx variable is an array which is pulled from the multidimensional array. That's pretty much it.
Alternatively I could make 10 separate querys to the database and increment 'AND playoffseed = 1' each time such as this...
$seed1 = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed = 1");
$seed2 = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed = 2");
but I didn't think querying the database 10 times was the way to go.
In the end I was hoping to get 10 variables ($seed1, $seed2,...$seedx) which would each be an array that includes (ttsUID, ttName, playoffseed).
still i am not sure how you want to store the data,but i hope this helps you
$playoffseedings = mysqli_query($con, "SELECT playoffseed, ttName, ttsUID
FROM standings
WHERE ttsUID = $season_view
AND playoffseed > 0
ORDER BY playoffseed
LIMIT 10");
while($row = mysqli_fetch_assoc($playoffseedings))
{
$seed[$row['ttName']] = $row['ttsUID'];
$seed[$row['playoffseed']] = $row['ttsUID'];
}
this will have data like below
(for example)
$seed['ttname123']= ttsuid123
$seed['playoffseed123']= ttsuid123
$seed['ttname456']= ttsuid456
$seed['playoffseed456']= ttsuid456
........
......
I was looking for a simple pagination script and found one here, this seems to be working just fine.
However, when i click on "2", as in page 2, it just shows the records of page 2 underneath those that are already there. So basically if I would click on page 214 it still shows all of the records on one page.
I am not very experienced with PHP so i couldn't figure out what was wrong with the paginator.class.php, hopefully someone here can.
This is the code on the page where it should do the pagination:
else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9;
$pages->paginate();
$query1 = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div> ';
};
echo ' ';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}
The paginator.class.php can be found on the website I just mentioned.
The issue lies in your query:
"SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit"
You need to determine what the value of $pages->limit is. It seems to me that instead of calculating how many records should be displayed on each page (let's say 10 for argument's sake) and then determining what page you're on and setting the LIMIT condition.
What it should be set to is something like this:
LIMIT 30, 10
That's for page 4 - it displays records 30-40, rather than what I suspect it's doing, which is
LIMIT 40
That line will simply show up to 40 records and not close the lower bound of the window.
FYI take a look at the MySQL SELECT syntax in the manual.