How to fetch the values from MySQL with switch case in PHP - php

I'm working with PHP and MySQL on a student registration project.
There is a table named programme from which I have to find the value of total no. of registration in every campus.
There are 4 campuses (Delhi, Noida, Jaipur and Mumbai) and 27 courses with 2 date of exam cycle (12-04-2014 and 07-06-2014).
I have to display total no. of registration in every campus for every course in every campus. for e.g. Delhi campus I made this query for first date:
$sql="select * from programme where campus1='delhi' && course1='Fashion Design (FD)' && examdate='12-04-2014'";
$result=mysql_query($sql);
$delhi=mysql_num_rows($result);
and it does echoe the result into the td for the first cycle.
But now I have to make a single query for 27 courses.
I've thought to do this via switch cases, but am unable to do it. How should I proceed?
Here is the Table format which I have to fill from database; it's for one course and similarly there are 26 other courses:
<table class="table table-bordered">
<tr>
<th> </th>
<th colspan="10">Course Wise Registration Report</th>
<th> </th>
</tr>
<tr>
<th>PROGRAMME</th>
<th colspan="2">Delhi</th>
<th colspan="2">Noida</th>
<th colspan="2">Jaipur</th>
<th colspan="2">Mumbai</th>
<th colspan="2">Cycle</th>
<th>TOTAL</th>
</tr>
<tr>
<td> </td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td>Cycle-1</td>
<td>Cycle-2</td>
<td> </td>
</tr>
<tr>
<td>UG-FD</td>
<td><?php echo $delhi; ?></td>
<td></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>UG-CD</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>UG-TD</td>
------------
</tr>
UG-JD
------------
<th>Total</th>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Here is DB table structure:
examdate campus1 course1 campus2 course2
Here is the format which i want :
Here is the screenshot of DB table programme:

You need to create a query something below.
SELECT COUNT(*) FROM programme
WHERE campus1 = 'delhi' AND examdate = '12-04-2014'
GROUP BY course1
Listen! this is not the final solution. Your tables structures seems not well organized. It should go through several normalization process. Until I have your complete set of table structure I can not give you a final answer.
For now.. just copy and run the above query in your PhpMyAdmin and see what it produce, then you will get an idea.
NOTE : I think it is not possible for you to explain well enough, if so, put some screenshots of your all tables grabbed form PhpMyAdmin and screenshot of your final result how it should looks like.

The query you want is:
SELECT campus1
,course1
,examdate
,count(*) AS count
FROM programme
WHERE examdate IN ('12-04-2014','07-06-2014')
GROUP BY campus1,course1,examdate
You may want to expand the WHERE clause if you don't want to include all campuses and courses.

here different solutions for ur problem
in the main page use combo and selecting campus, list details of that campus below that
combo using a ajax page
combo posting page you just receive the value of that combo means compus name and add to
ur sql directly.
If we use switch we want to enter a input to switch. depending on this input we can
change the output in each case.

Related

How to make sum of all rows in mysql OR how to show show data in following way using mysql

This data is showing in MySql Database
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
10
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
5
</td>
</tr>
</table>
and I want to show data on webpage like this
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
15
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
</table>
Note that in database "New Zealand" showing 2 time in Database but on webpage it showing 1 time and also its score in sum
please tell me how can I do this using mysqli
Thanks in Advance.
Without your query I can't use your table or field names, but it would look something like this:
SELECT `team`, SUM(`score`) AS `score_sum` FROM `teams` GROUP BY `team`
This groups the rows by team and for each group, the scores are summed for that team. You would then access the team name using team and the total score for each team using score_sum in your PHP code. You'll have to provide more details like your query and code if this doesn't help.
I'll give a suggestion for your table , considering that you have already created soccer table with three columns ID,Team,Score . regardless a way you insert a data in this table , here how to get it properly
<?php
include("connection.php");
$team = "select distinct team from soccer";
$team2 = mysqli_query($mysqli,$team);
$up = "0";
$id ="1";
echo "<table><tr><td>ID</td><td>TEAM</td><td>SCORE</td></tr><tr>";
while ($teamres = $team2->fetch_array()){
echo "<td>".$id."</td><td>".$teamres['team']."</td>";
$score = mysqli_query($mysqli,"SELECT sum(score) FROM soccer where team ='".$teamres['team']."'");
$score2 = mysqli_fetch_row($score);
$scorere = $score2[0];
echo "<td>".$scorere."</td>";
echo "</tr>";
$id++;
$up++;
}
echo "</table>";
?>

how to show the count of particular name? & show it page using php sql?

i have a agent table
& where all agent details hai been display.
So along with the details i want to show count the agent name has been displayed in the table in same table.
i tried this query
Select count(name) from agent;
but it shows whole table count.
i want result to be this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>1</td>
</tr>
<tr>
<td>amit</td>
<td>4</td>
</tr>
<tr>
<td>sushant</td>
<td>8</td>
</tr>
</table>
2/5
Saurabh 4/5
bt wat i m getting is this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>13</td>
</tr>
<tr>
<td>amit</td>
<td>13</td>
</tr>
<tr>
<td>sushant</td>
<td>13</td>
</tr>
</table>
so how to show particular name count in table row in php page??
Try this
select count(name)as count,name from agent group by name
Update code
<?php $query=mysql_query("SELECT count(name) as count,phone,email,t_id,name FROM transfer WHERE agent_id='$id' group by name ORDER BY t_id DESC"); while($row=mysql_fetch_array($query)) {
echo $row['name'];
echo $row['count'];
}
?>

How to get dynamic html-table-content

This is the output of one of my file
<table>
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
I want to get the content of Description x with php and then I want to read out the Content x. One of my problem is, that before grapping the content I don't know how many description-td's I have.
Should DOM / getElementsBy ... work for my problem?
Best regards,
Susan
Here you have a solution with JQUERY. Try if yourself
In this example we show in an alert dialog the content of the description number 3.
HTML:
<table id="mytable">
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
</table>
JQUERY:
// Search content of description number 3
$(document).ready(extractContent(3));
function extractContent(num) {
$('td', $('#mytable')).each(function() {
// The text in the first column must match " Description num "
if ($(this).text() == " Description " + num + " ")
alert("Content found: "+$(this).next().text());
});
}

Unable To View All Entries In Table

I wonder whether someone may be able to help me please.
Firstly my apologies. I would have liked to provide a link to this rather than printing the code below, but I need this to run off my live server tables.
I'm using the code below to generate a table of all location records pertinent to the current user.
<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
<table width="864" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="17"></th>
<th width="99"><div align="center">Location Name</div></th>
<th width="287"><div align="left">Location Address</div></th>
<th width="88"><div align="center">No. Of Finds Made </div></th>
<th width="86"></th>
<th width="72"></th>
<th width="84"></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT l.locationid, f.locationid, l.locationname, l.userid, l.returnedaddress, count(f.locationid) as totalfinds FROM detectinglocations as l left join finds as f on l.locationid=f.locationid WHERE l.userid='$idnum' ORDER BY l.locationname";
$result = mysql_query($query) or die('error');
if (mysql_num_rows($result) == 0)
echo"<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan='6'><div align='center'><strong>There are no records set up</strong></div></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
else
{
while($obj=mysql_fetch_object($result))
{
?>
<tr>
<td><input type="hidden" name="lid" value="<?php echo $obj->locationid;?>"/></td>
<td><div align="center"><?php echo $obj->locationname;?></div></td>
<td><div align="left"><?php echo $obj->returnedaddress;?></div></td>
<td><div align="center"><?php echo $obj->totalfinds;?></div></td>
<td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Images"/></div></td>
<td width="129"><div align="left"><input name="type" type="submit" value="View Location Finds"/></div></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Although the query is retrieving the right information and the buttons on the row work, the problem I'm having is that although there should 3 records shown in the list, only the first is shown.
I'm the first to admit that I'm certainly no expert when it comes to PHP, but I've been working on this for days and written the script many, many times, but I just can't seem to find a solution.
I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.
As pointed out by both #Bulk and #ShawnVaser there was a problem with my query. This is the solution:
Query
$query = "SELECT l.*, COUNT(f.locationid) as totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '28' GROUP BY l.locationname";
$result = mysql_query($query);

PHP MySQL populating values from database

lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';

Categories