I am a neophyte programmer in php and I am always seeking for the solution of this problem. If anyone has an idea pls. post your answer and I am thankful for your great ideas to solve this stuff.
In my database table I have data like this:
In my php page I want to present in this way using html table.
Could anyone help me doing this stuff? Thank you very much…
I think you are searching for the functionality of GROUP_CONCAT in mysql (docs). You would get something like:
SELECT name, GROUP_CONCAT( week ), GROUP_CONCAT( there )
FROM presence
GROUP BY name;
which will return the following results you can parse using explode() in php (docs):
Andrew 4th,1st,3rd,2nd Present,Present,Present,Present
John 1st,4th,3rd,2nd Absent,Present,Present,Present
Mark 2nd,3rd,1st,4th Present,Present,Present,Present
Micheal 2nd,3rd,4th,1st Absent,Absent,Absent,Present
On a side note: If you haven't settled on a database scheme yet, it might be better to use an int for the week number column, as it is more reliable when sorting and easier to manipulate.
Sqlfiddle: http://sqlfiddle.com/#!2/fc785/1
Try this:
$SQL = "select NAME, WEEK, STATUS from tblattendance order by NAME, SUBSTRING(WEEK,1,LENGTH(WEEK) - 2) ASC";
$data = $db->query($SQL);
$last_name = "";
echo '<table><tr><th>NAME</th><th>1st WEEK</th><th>2nd WEEK</th><th>3rd WEEK</th><th>4th WEEK</th>';
while($row = $data->fetch_assoc()){
if($last_name != $row["NAME"]){
$last_name = $row["NAME"];
echo '</tr>';
echo '<tr>';
echo '<td>'.$row["NAME"].'</td>';
}
echo '<td>'.$row["STATUS"].'</td>';
}
echo '</tr></table>';
Try this :
$sql="select distinct name from tblattendance;";
$res=$mysqli->query($sql);
if($res){
while($row=$res->fetch_assoc()){
$name=$row['name'];
$sql="select week, status from tblattendance where name='$name';";
$res1=$mysqli->query($sql);
}
}
Related
I am trying to make a leaderboard and sort my data by kills, but when I try to make it so it only grabs name, kill, death it doesnt grab anything but when I have it grab it all it works. Anyone know why? Code is below please assist.
<?php
$query = $koneksi->prepare("SELECT * from `player`");
$query->execute();
if($query->rowCount() == 0)
I am grabbing my mysql data here, if I change the * to the data I need no data is displayed.
echo "<tr><td colspan='6'><small>There's no player on ban list</small></td></tr>";
}
while($data = $query->fetch())
{
echo "<tr><td>".$data['name']."</td>";
echo "<td>".$data['kill']."</td>";
echo "<td>".$data['death']."</td>";
$kd = $data['kill'] / $data['death'];
echo "<td>".$kd."</td></tr>";
}
?>
Is it something to do with this or is something wrong? I am really confused.
Here you have to use bind_result() and in that you have to pass the number of parameters which is equal to your number of field from your player table.
Because here you are fetching data using select * query.
Currently i'm using php and mysql to fetch downlines of a particular userid. Is there any way i can just use mysql to do that. My code is......
function downlineSearch($mid, $s)
{
global $nums; global $str;
$q = $cn->query("SELECT userid, CONCAT(lastname,' ',firstname,' ',middlename) AS name FROM clientsdata WHERE (recid LIKE '%$s%' OR lastname LIKE '%$s%' OR firstname LIKE '%$s%' OR middlename LIKE '%$s%') AND sponsor = $mid");
while($r = $q->fetch_array(MYSQL_ASSOC))
{
$nums += 1;
$str .= "<tr><td valign='middle'>NRN".$r['userid']."CCN</td><td valign='middle'>".$r['name']."</td></tr>";
downlineSearch($r['userid'], $s);
}
}
$str = '';
downlineSearch($mid, 'janet');
echo '<table id="tblSearch" width="100%">';
echo "<tr><td colspan='4' align='center' style='padding-top:10px;'><h3> $nums records found for [ <span style='color:red'>".$_POST['s']."</span> ]</h3></td></tr>";
echo $str;
echo '</table>';`
PLEASE ANY HELP WOULD BE HIGHLY APPRECIATED. THANKS
My guess it that you need to set the value of $cn, probably something like that:
$cn = new mysqli;
Does that help?
What is the error message you are getting?
Do you know in which line the error happens?
Do you have ability to debug line-by-line and view variables? If not, add several "echo" statements, to view in browser what's going on. Or, use error_log(), to display info in PHP error log, if you have access to it.
(I'm sorry about asking for clarification in an "answer", but the system does not let me add comments to other people's question - yet. As soon as you add some clarification, I will revise this "answer" to be actual answer to your question.)
Ok so I've searched and searched but still struggling to resolve my problem. This is my current php coding:
$show = "Select effectiveness, round((Count(effectiveness)* 100 / (Select Count(*) From acupuncture))) as Score
From acupuncture
Group By effectiveness
ORDER BY Score DESC";
$result = mysql_query ($show);
WHILE($show = mysql_fetch_array($result))
{
$field1 = $show[effectiveness];
$field2 = $show[Score];
echo "$field1: ";
echo "$field2%<br><br>";
}
In addition to displaying the above I would also love to display the number of rows in the table. I know the sql code is:
"SELECT COUNT(id) AS entries FROM acupuncture"
Problem is when I try to input this into my php page I keep getting errors. I want to show both SELECT statement results on the one php page. If someone can help I would greatly appreciate it.
Thank you
Shikz
All good, problem has been fixed. Thanks for all your help :) P.S. This is the code I inputted:
$size = #mysql_query("SELECT COUNT(*) AS `total` FROM acupuncture");
$query = mysql_fetch_array($size);
echo "Number of entries: ";
echo $query['total'];
echo "<br><br>";
I was writing up the php code incorrectly before, but now all good. Thanks again.
Try this:
while($show = mysql_fetch_assoc($result))
{
$field1 = $show['effectiveness'];
$field2 = $show['Score'];
echo "$field1: ";
echo "$field2%<br/><br/>";
}
To cound all rows found read here
Small hints:
Please stop using mysql, it is deprecated.
Use mysqli or PDO instead.
ALWAYS use quotes when using string-indexed array
Make change in
WHILE($show = mysql_fetch_array($result))
{
$field1 = $show[effectiveness];
$field2 = $show[Score];
echo "$field1: ";
echo "$field2%<br><br>";
}
TO
WHILE($row= mysql_fetch_array($result))
{
$field1 = $row[effectiveness];
$field2 = $row[Score];
echo "$field1: ";
echo "$field2%<br><br>";
}
When I click on any link it opens all movies in my database. I want only that movie which begins with that letter and I don't know where I've made a mistake. Here is my code:
$azRange = range('A', 'Z');
foreach ($azRange as $letter){
echo ''.$letter.' | ';
}
if(isset($_GET["task"]) && $_GET["task"] == "view"){
$naslov = $_GET['naslov'];
$query = "SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
ORDER BY naslov";
$result = mysql_query($query)
or die ('SQL Greska: '.mysql_error());
if($result){
while($filmovi = mysql_fetch_array($result)){
echo '<center><b>';
echo '<td><img src="img/'.$filmovi["slika"].'" border="0" width="100" /></td>';
echo '</br>';
echo '<td>'.$filmovi["naslov"].'</td>';
echo '<td> ('.$filmovi["godina"].')</td>';
echo '<br>';
echo '<td>Trajanje: '.$filmovi["trajanje"].' min</td>';
echo '</b></center>';
echo '</tr>';
}
You are not passing the letter to the database query at any point.
$query =
"SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
WHERE naslov LIKE '$naslov%'
ORDER BY naslov";
Your query
$query = "SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
ORDER BY naslov";
is fetching all the movies from the database. There is no filtering here. Add some where conditions to this query and you'll get the expected result.
Changing to this query might help:
SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
WHERE `naslov` LIKE '{$naslov}%'
ORDER BY naslov
Since others have already answered your question (missing WHERE clause), I just want to mention that the <center> HTML tag is deprecated, and you should use CSS instead.
The mysql driver for PHP is also outdated, so instead of using:
mysql_query($query);
you should use
mysqli_query($link, $query);
for better security, OOP support, prepared statements, and transactions.
You can read about it here
Even if you are a beginner and you don't care about what those features mean, you should try and get into the habit of using mysqli anyway, so that when the day comes that you learn to appreciate it, you don't have to go back and update all of your code.
I want to sort my result form mysql by date. I use the query like this:
<?php
$date = $db->get_query("select distinct created_date from comments");
$condate = '';
for($i=0; $i < count($date); $i++)
{
$condate = $date[$i]['created_date'];
$data = $db->get_query("select id,created_date from comments where created_date='$condate' order by created_date");
?>
<table border='1' style="float: left; margin-left: 5px;">
<?php
for($j=0; $j<count($data); $j++)
{
echo '<tr><td>';
echo $data[$j]['id'] ;
echo '</td><td>';
echo $data[$j]['created_date'];
echo '</td></tr>';
}
?>
</table>
<?php
}
?>
This query produce result like this:
2009-07-10
2009-07-10
2009-08-21
2009-07-29
2009-08-15
The result is not sorted.
I want to see the result is:
2009-07-10
2009-07-10
2009-07-29
2009-08-15
2009-08-29
with separated table order by created-date.
I want to know sorting date in mysql result .In this case $condate is variable for validate condition.The value of $condate is all created_date in comments table. I produce this as within loop and set the value is.
Please help me!
If you're only selecting results from a single date, then there's nothing to sort by. What exactly is the WHERE condition doing?
Edit: Now that you've posted your code, I can offer a suggestion. Your original code is running a separate query for each different date. What you really want is a single query that returns the results for all dates, but in a specific order, which is what the query in the code below does. Try this instead:
<?php
$data = $db->get_query("select id,created_date from comments order by created_date");
?>
<table border='1' style="float: left; margin-left: 5px;">
<?php
for($j=0; $j<count($data); $j++)
{
echo '<tr><td>';
echo $data[$j]['id'] ;
echo '</td><td>';
echo $data[$j]['created_date'];
echo '</td></tr>';
}
?>
</table>
Note that you already had all of this in your original code! You just managed to convince yourself that the task was more complicated than it actually was. :)
Because you put = in the where clause, so all the records will be in the same day and the sort will not be useful.
Edit
Are you using date field type for created_date field or string? if it's string so that may cause your problem...
Maybe you have your date field is stored as a "string" and not something like datetime?
If you check with:
describe table_name;
How is your date stored?
You are looping through results that aren't ordered to generate the dates you are asking for in the second query. Up near the top you were querying:
$date = $db->get_query("select distinct created_date from comments");
// should be (and excuse the keyword capitalization, I just think its easier to read)
$date = $db->get_query("SELECT DISTINCT created_date FROM comments ORDER BY created_date");
You just put your ORDER BY clause on the wrong query.
I dont think you need to execute 2 queries.
Try executing
$data = $db->get_query("select DISTINCT id,created_date from comments where created_date='$condate' order by created_date");
I think you had all built-in in your code but you just got confused a little.
Do let me know whether this solved your problem or not.