I am using AJAX Pagination for displaying my records in application from MySQL using PHP.
I have done that successfully but just need one solution:
I need to display a Serial No while displaying records like:
----------------------------
S.No Name Country
----------------------------
1 Sandeep India
2 Rahul Japan
3 Riya China
4 Sohan India
...
50 James USA
If I have set to show 20 results per page while paginating the first page shows serial no. 1 to 20 , 2nd page shows serial no 1 to 20 and 3rd page shows serial no 1 to 10.
But I want to show serial no. 1 - 20 in first page , 21- 40 in second page , 41- 50 in 3rd page.
How can I do this in PHP?
Here is my code:
$ssql = $ssql_select.$ssql_where." order by reg_date desc LIMIT $Page_Start , $Per_Page";
$rs=mysql_query($ssql, $cn) or die("MySQL error: ".mysql_error());
$ctr = 1;
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>".$ctr."</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
Try
...
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>" . $ctr + $Page_Start . "</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
...
Related
I have 3 different tables
table 1 keep general scores over the year
table 2 and 3 is event specific
only common field in all 3 is a member ID
I need to select 5 top score results for each member from table1 combine them with 1 specific result from table 2 and 3
I managed to get everything together in a temp table, but cnt get the output the way I need it
example what i have and need.
Original code
$prevQuery = "SELECT distinct member_id FROM scores";
$prevResult = $conn->query($prevQuery);
while($row = $prevResult->fetch_assoc()) {
$scores=("SELECT member_id,event_id,event_date,event_score FROM scores where member_id = ".$id." ORDER BY event_score DESC LIMIT 5");
Query:
select * from temp_table order by mem_id asc
PHP:
you can simple do your expected result in your application like this
<?php
$result = array(array('mem_id'=>1,'location'=>'A','date'=>'20/05/2017','score'=>100),array('mem_id'=>1,'location'=>'B','date'=>'21/05/2017','score'=>103),array('mem_id'=>1,'location'=>'C','date'=>'22/05/2017','score'=>106),array('mem_id'=>1,'location'=>'C','date'=>'23/05/2017','score'=>108),
array('mem_id'=>2,'location'=>'A','date'=>'20/05/2017','score'=>105),array('mem_id'=>2,'location'=>'B','date'=>'21/05/2017','score'=>109),array('mem_id'=>2,'location'=>'C','date'=>'22/05/2017','score'=>111),array('mem_id'=>2,'location'=>'C','date'=>'23/05/2017','score'=>110));
$new_result=array();
foreach($result as $key=>$row)
{
$new_result[$row['mem_id']][]=$row;
}
echo "<table border='1px'>";
echo "<thead><tr><th>S.No</th><th>date</th><th>score1</th><th>date</th><th>score2</th><th>date</th><th>score3</th><th>date</th><th>score4</th></tr>";
echo "<tbody>";
foreach($new_result as $key=>$row)
{
echo "<tr><td>".$key."</td>";
foreach($row as $key1=>$row1)
{
echo "<td>".$row1['date']."</td>";
echo "<td>".$row1['score']."</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
OUTPUT :
S.No date score1 date score2 date score3 date score4
1 20/05/2017 100 21/05/2017 103 22/05/2017 106 23/05/2017 108
2 20/05/2017 105 21/05/2017 109 22/05/2017 111 23/05/2017 110
I would like to display SQL results, each line with html link, where each link contains the table id.
My table name : fruits
table_id content description price
---------------------------------------------------
1 apple tastes good 3 usd
2 peach grows on tree 4 usd
3 plump very purple 1 usd
It should display
Fruits
apple | tastes good
peach | grows on tree
plump | very purple
(Note: I haven't fetched price at this point)
My code so far:
$result = mysql_query("SELECT table_id,content,decription FROM fruits",$conn);
echo table
while ($row = mysql_fetch_row($res)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
What I want to achieve instead of echo :
a href="link to fetch id 1">apple | tastes good
a href="link to fetch id 2">peach | grows on tree
a href="link to fetch id 3">plump | very purple
When i mean "link to fetch id 1", i mean like href="www.application.com/another.php and some way to pass fetch id 1 or 2 or 3 to another php file.
How can i make these links ?
How can I catch the passed id in another php file ?
Thank you for your help.
Something like
while ( false!=($row=mysql_fetch_assoc($res)) ) {
echo '
<tr>
<td><a href="details.php?id=', urlencode($row['table_id']), '">',
htmlspecialchars($row['content']),
'</td>
<td>', htmlspecialchars($row['decription']), '</td>
</tr>
';
}
you'd fetch the table_id in details.php from $_GET['id']. Test its existence first via isset()
For making the entire row "clickable" see Link entire table row?
Btw: the mysql_* extension is deprecated. Pick another API: mysqli or PDO_mysql.
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
And while you're at it and since you most likely are going to use _GET['id'] in another query, take a look at prepared statements and (named) parameters.
I have menu that displays entries using php and mysql. What I'm trying to do is to get a list of products that equal clothing for example then shoes and so on. Theirs about 4 main types, this value can't be changed.
So:
$query = mysql_query("SELECT * FROM products WHERE type = 'Clothing'")or die(mysql_error());
Instead of looping through I'll just repeat the process 4 times.
After this I want to display each main category and sub category for each type of clothing with the products table, so that it displays like this.
Clothing
Main Category 1
(1)Sub 1
(35)Sub 2
(4)Sub 3
Main Category 2
(1)Sub 1
(35)Sub 2
(4)Sub 3
Shoes
Main Category 1
(1)Sub 1
(35)Sub 2
(4)Sub 3
Main Category 2
(1)Sub 1
(35)Sub 2
(4)Sub 3
And so on
I get a list of each main catogroy in the DB form a different table and loop through
$get_cats = mysql_query("SELECT * FROM main_cats")or die(mysql_error());
//loop through each
while($main_cat = mysql_fetch_assoc($get_cats)){
//count main cat in products
$check = mysql_real_escape_string($main_cat['cat']);
$p_main_count = mysql_query("SELECT * FROM products WHERE cats = '$check' ORDER BY cats")or die(mysql_error());
//this would get an array of each product that has that main category
}
This looks all well and good but I can't think of a way how to display the data in the format I need it.
The products table has 3 main columns for this:
type
cats (main category)
sub cats (contains string 1,2,4 of multiple sub categorys)
Is there a way selecting and groups each main category and then displaying and counting the sub category's for each main category.
W3Schools has a really good tutorial for counting and displaying MySql Database entries. Here's a snippet of how you could try to display the Data in a table.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM main_cats");
while($row = mysqli_fetch_array($result))
{
echo $row['type'] . " " . $row['cats'] . " " . $row['subcats'];
echo "<br>";
}
?>
Lets say row 1 has the data Type: 1 Cats: 2 SubCats: 3
and row 2 has the data Type:2 Cats: 4 SubCats: 6
This would output the data like this
1 2 3
2 4 6
You can easily use the tables from HTML to make this look alot nicer.
echo "<table border="1">";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['type'] . "</td><td> " . $row['cats'] . "</td><td> " . $row['subcats'] . "</td>";
echo "</tr>";
}
echo "</table>";
This would output in the same order but in a table.
Hope this helps. Also if you want any good references http://w3schools.com is a very good website.
I have a project on student ranking. The sample school does three terminal exam in a year. I created three different tables for each terminal exam, (i.e. firstermsar for first, secondtermsar for second, thirdtermsar for third term respectively).
Table structure is like this:
id studentid matca1 matca2 matexam engca1 engca2 engexam
1 2 15 14 40 12 10 60
2 1 10 5 56 9 13 35
3 4 11 9 45 14 17 40
4 5 14 1 50 20 0 60
Students with id 4 and 5 are in class1 while students 2 and 1 are in in class2. I have a seperate table for classes and also seperate table for student profile info.
Now, I already output the result slip but without ranks which looks like this
StudentId: 2 Full Name: Fawaz James Class: Pry 2 Overall Position: -
1stAss 2ndAss ExamScore Grade Position Rmks
Mark Obtainable 20 20 60
English 12 10 60 A - Excellent
Mathematics 15 14 40 B - V. Good
Though am a starter, and my code is a mess, its provided underneath. Moreover I need help with calculating the overall position on class basis and subject position on class basis. Please help me as my project depends on this for approval and am in my deadline week before I got introduced to stack Overflow.
$query = "SELECT * FROM firsttermsars
WHERE studentID=$stuID AND year=\"$_SESSION[year]\""; #27
$result = mysql_query($query)
or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<h2> First Term Stint Report Sheet for $namet during the SESSION $year</h2>";
echo "<table class='table1'>";
echo "<tr>";
echo "<td></td>";
echo "<td><b>1st Assessment</b></td><td><b>2nd Assessment</b></td>
<td><b>Mid Term<br />Exam</b></td><td><b>Mid Term<br />Total Score</b></td><td><b>Grade<br />Point</b></td></tr>";
echo "<tr> <td><b>Mark Obtainable</b></td><td><b>30</b></td><td><b>20</b></td><td><b>50</b></td><td><b>100</b></td><td><b>||=||</b></td></tr><tr>";
$tetal = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
if ($tetal >="1") {
$q2 = "1";
$sq2 = "100";
echo "<td>English Language: </td>";
echo "<td>{$row['eng1']}</td><td>{$row['thirdengtest']}</td><td>{$row['thirdengexam']}</td> <td>";
$total1 = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
echo $total1."</td><td>";
if ($total1>="75")
echo "A1";
elseif ($total1>="70")
echo "B2";
elseif ($total1>="65")
echo "B3";
elseif ($total1>="60")
echo "C4";
elseif ($total1>="55")
echo "C5";
elseif ($total1>="50")
echo "C6";
elseif ($total1>="45")
echo "D7";
elseif ($total1>="40")
echo "E8";
elseif ($total1>="1")
echo "F9";
elseif ($total1=="0")
echo "-";
else
echo "F";
echo "</td></tr>";
}
$tetal = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
if ($tetal >="1") {
$q3 = "1";
$sq3 = "100";
echo "<tr>";
echo "<td>Mathematics </td>";
echo "<td>{$row['math1']}</td><td>{$row['thirdmathtest']}</td> <td>{$row['thirdmathexam']}</td> <td>";
$total2 = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
echo $total2."</td><td>";
if ($total2>="75") echo "A1"; elseif ($total2>="70") echo "B2"; elseif ($total2>="65") echo "B3"; elseif ($total2>="60") echo "C4"; elseif ($total2>="55") echo "C5"; elseif ($total2>="50") echo "C6"; elseif ($total2>="45") echo "D7"; elseif ($total2>="40") echo "E8"; elseif ($total2>="1") echo "F9"; elseif ($total2=="0") echo "-"; else echo "F";
echo "</td></tr>";
}
?>
I hope you can help me like this, and hope is not complicated. I wanted to add the picture of the output but its complaining i don't have the required reputation
Firstly, in the database, make a new column for total('Total') and ('Grade'), and when you are submitting the marks details to your database, add the marks and grade and then submit the total to the database, instead of calculating them while retrieving the information.
Second, when you are selecting students from the database, use the SQL command:
SELECT * FROM firsttermsars ORDER BY Total DESC
More info on ORDER BY
This will automatically return the list of students in descending order of their total marks.
Similarly, for subject ranking, you can have a separate table, where ranking is done on subject marks
Then, while retrieving the data,
<table>
<?php
while ($i<$num) //num is number of rows
{
?>
<tr><td><?php echo mysql_result($result,$i,"Name"); ?></td>
//... Similarly for all clumns
<?php
}
?>
</table>
This should make the table with all students ranked according to their total marks. Similarly you can make a table for ranking by subject marks.
This is how i would solve the problem. Hope I helped!
Although i am late, but it may help some one.
First of All you should calculate Obtained marks and store in database table.
For simplicity i am showing just 3 column.
Id Name Obtained
1 A 100
2 B 58
3 L 88
4 F 102
5 C 99
Insert the row in table , then run this query to get Position of student.
SELECT id, name, Obtained, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM scores )
) AS position
FROM scores
WHERE id = 5
Detail here https://dba.stackexchange.com/questions/13703/get-the-rank-of-a-user-in-a-score-table
I'm now hopeless about this problem and that's why I'm here. I'm kind of a starter in PHP and mysql programming. I searched for a solution on the web but I wasn't succeeded. I'm working on a project what is a car repairing administration system. This is a part of it that I need.
I have two tables:
repairs (j_id /this is the primary key/, rendszam, javitas, megjegyzes, datum)
and
pictures (kep_id /this is the primary key/, j_id /it's from repairs j_id/, kepnev)
I need to display a result in a table, where I can see that which of the repairs have a picture in the pictures table, but without redundancy, so I don't want multiple repair rows that are similar to eachother, but one repair row with multiple picture columns after it.
What I have done already:
<?php
$sql = "SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>" . $row[$i] . "</td>";
echo "<td>" . $row[$i + 1] . "</td>";
echo "<td>" . $row[$i + 2] . "</td>";
echo "<td>" . $row[$i + 3] . "</td>";
echo "<td>" . $row[$i + 4] . "</td>";
echo "<td>" . $row[$i + 5] . "</td>";
}
?>
This one displays information like this:
repair id | picture
1 | fdgdfg.jpg
1 | fgdfg.jpg
1 | fghh.jpg
25 | dfg.jpg
25 | jkjk.jpg
But I don't want to have multiple repair rows but one repair row with multiple pictures after it.
I tried this:
$sql2="SELECT DISTINCT $tbl_name2.j_id, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result2=mysql_query($sql2) or die(mysql_error());
$sql="SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id GROUP BY j_id;
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row[$i]."</td>";
echo "<td>".$row[$i+1]."</td>";
echo "<td>".$row[$i+2]."</td>";
echo "<td>".$row[$i+3]."</td>";
echo "<td>".$row[$i+4]."</td>";
echo "<td>".$row[$i+5]."</td>";
while($sor=mysql_fetch_array($result2))
{
if($sor['j_id']==$row[$i]){
echo "<td><a href=kepek/".$sor['kepnev']." target=_blank>".$sor['kepnev']."</a></td>";
}
}
}
The sql statement differs in the group by in $sql, but this one displays the results like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 |
So it's just not continuing after the first repair id, not showing the repair pictures for repair id 25.
I don't know, how can I get it right. I need a table which displays information like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 | dfg.jpg | jkjk.jpg
Could you help me out in this?
This sounds fairly straightforward, looks like you're off to a good start.
I've done something similar in the past, and I just used a variable to keep track of the current ID being displayed and the previously displayed ID. Here's a quick code sample to outline what I mean by that.
while($row=mysql_fetch_array($result))
{
$currentID=$row[$i];
if ($currentID != $lastID) {
echo "</tr><tr><td>".$row[$i]."</td>";
} else {
echo "<td>".$row[$i]."</td>";
}
$lastID=$row[$i];
}
So, the idea is to keep track of what the last ID was. In the next row, if the ID is the same, just output <td>"variable goes here"</td> to keep it on the same column. If the ID changed, end the row and create a new row using </tr><tr>". Feel free to comment if you need more help.