Show A Centre's Students in their login page

Please help me out of this problem.

I’ve a table named “students” having rows like “registration”, “name”, “gender”, “address” etc. (registration" is the “UNIQUE KEY”)

I’ve made our sub-centre’s login page where they can see how many total number of students they have admitted.

But I want to display the students’ details like their name, address in a page. Suppose a centre has admitted 10 students, then in their login page they can see their students’ details in a table. I’ve made the code but it only shows the registration number. Please help. Here is the code:

[php]<?php
include ‘dbc.php’;
/page_protect();
?>

My Students
 
<?php if (isset($_SESSION['user_id'])) { $admitted = $_SESSION['user_name']; $query = mysql_query("select * from students where centre='$admitted'") or die(mysql_error()); list($admition) = mysql_fetch_row($query); ?>

My Account

My Account
My Students
Settings
Logout

You can add more links here for users

<?php } if (checkAdmin()) { ?>

Admin CP

<?php } ?>

 

 

 

 

Welcome <?php echo $_SESSION['user_name'];?>

<?php if (isset($_GET['msg'])) { echo "
$_GET[msg]
"; } ?>

Students List

 
<?php echo $admition;?>
[/php]

You need to update the code to use either mysqli or pdo. mysql_* is deprecated and will be removed from PHP

[hr]

You also seem to be using tables for layout, which is considered bad practise. I’d suggest looking into using appropriate elements, a good start is to just use bootstrap or some other ui framework.

[hr]

Can the students only be admitted to one centre?

[hr]

Either way student.centre should still be the centre id instead of the centre name. So you get something like this

student: id | name | gender | address | centre_id _________________________________| | centre: id | name | description

[hr]

For your question though you don’t fetch all the students, you just fetch the first column of the first result

It should be something like this:

[php]$students = array();
$admitted = $_SESSION[‘user_name’];
$query = mysql_query(“select * from students where centre=’$admitted’”) or die(mysql_error());
while($row = mysql_fetch_row($query)) {
// add each row to the students array
$students[] = $row;
}[/php]

[php]<?php foreach ($students as $student) { ?>

<?= $student['name'] ?> <?= $student['address'] ?> <?php } ?>[/php]

But the following code shows nothing. Only blank table created.

[php]<?php foreach ($students as $student) { ?>

<?= $student['name'] ?> <?= $student['address'] ?> <?php } ?>[/php]

How does your data look?

If you’re unsure then paste the output you get from this:

[php]// add this
echo ‘

’;
print_r($students);
die();
// before this <?php foreach ($students as $student) { ?>[/php]

This is what the echo ‘

’ code displays:

I want to display only “Registration [ 0 ]”, “Name [2]”, “DOB [15]” and “Mobile [16]”

[php]Array
(
[0] => Array
(
[0] => nifeA002
[1] => 0
[2] => MD HOSSAIN
[3] => MASURA BEGUM
[4] => Male
[5] => General
[6] => Class X
[7] => GODA MAJPARA
[8] => P.O.- LAKURDI
[9] => Burdwan
[10] => 713102
[11] => NBCE
[12] => ADVANCE DIPLOMA IN INFORMATION TECHNOLOGY
[13] => AIT0052
[14] => 1 Year
[15] => 08-01-2015
[16] => 98*****860
[17] => 02-02-1998
[18] => Computer Universe
[19] => photo/nifeA002.jpg
)

[1] => Array
    (
        [0] => nifeA001
        [1] => 0
        [2] => SK RAJIBUR RAHAMAN
        [3] => SK AZMIRA BEGUM
        [4] => Male
        [5] => General
        [6] => Class X
        [7] => GODA KOYET TALA
        [8] => P.O.- LAKURDI
        [9] => Burdwan
        [10] => 713102
        [11] => NBCE
        [12] => ADVANCE DIPLOMA IN INFORMATION TECHNOLOGY
        [13] => AIT0052
        [14] => 1 Year
        [15] => 08-01-2015
        [16] => 91*****623
        [17] => 16-10-1998
        [18] => Computer Universe
        [19] => photo/nifeA001.jpg
    )

[2] => Array
    (
        [0] => nifeA003
        [1] => 0
        [2] => KALIPADA RAJAK
        [3] => BABITA RAJAK
        [4] => Male
        [5] => SC
        [6] => Class X
        [7] => KALNA GATE
        [8] => ADI BASI PARA
        [9] => B
        [10] => 713101
        [11] => NBCE
        [12] => ADVANCE DIPLOMA IN INFORMATION TECHNOLOGY
        [13] => AIT0052
        [14] => 1 Year
        [15] => 12-01-2015
        [16] => 98*****986
        [17] => 20-11-1998
        [18] => Computer Universe
        [19] => photo/nifeA003.jpg
    )

)[/php]

Ah, as you see the mysql_fetch_row function returns row values in an index based array. What we would like is an associative array that includes the column names as keys.

Try changing mysql_fetch_row to mysql_fetch_assoc and see what it does to your data set

Thank You Sir… It works

Sponsor our Newsletter | Privacy Policy | Terms of Service