Arun Kumaresh
Arun Kumaresh

Reputation: 6311

Data is printing multiple time in php?

enter image description here

The course name php is available in the table one time but when I retrieve it from table it printing multiple time. I have tried the following code - please help me what's wrong with the code? (if I remove the drop down which is available below the heading it is printing single time in the table)

<?php
include("session.php");
?>
<?php
include("../view/common/head.php");
?>
<?php
  include('../view/common/tab.php');
  ?>
<body class="bg-color">

<div class="container">
<h1> List Of Courses</h1>
<br/>
<?php

if (isset($_SESSION['message1']))
{
 echo $_SESSION['message1'];
 unset($_SESSION['message1']);
}
?>
<?php
if (isset($_SESSION['courseupdated']))
{
 echo $_SESSION['courseupdated'];
 unset($_SESSION['courseupdated']);
}
?>
<?php

if (isset($_SESSION['deletecourse']))
{
 echo $_SESSION['deletecourse'];
 unset($_SESSION['deletecourse']);
}
?>
<?php

if (isset($_SESSION['notdelete']))
{
 echo $_SESSION['notdelete'];
 unset($_SESSION['notdelete']);
}
?> <div class="col-md-6 form-group">
   <select class="form-control">
<?php
include('../model/functions.php');
$table="courses";
$condition="";
$drop=Selectdata($table,$condition);
foreach($selectarray as $drop)
{
  echo '<option value="'.$drop['course_id'].'">'.$drop['course_name'].'</option>';
}
?>
</select>       
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Sno</th>
 <th>Course Name</th>
 <th>Course Description</th>
 <th>Action</th>
</tr>
</thead>
<tbody>

  <?php
  $per_page=5;

if (isset($_GET['page'])) {

$page = $_GET['page'];

}

else {

$page=1;

}


$table="courses";
$per_page=5;
$start_from = ($page-1) * $per_page;
$condition="limit ".$start_from.",".$per_page." ";
$limit=Selectdata($table,$condition);
if($rowcount==0)
{
  echo "no records found";
}
else
{
$sno=($page-1) * $per_page;
 foreach($selectarray as $course){
    $sno++;
   echo '<tr>';
   echo '<td>'.$sno.'</td>';
   echo '<td>'.$course['course_name'].'</td>';
   echo '<td>'.$course['course_description'].'</td>';
   echo '<td><a href="editcourse.php?id='.$course['course_id'].'"><button type="submit">edit</button></a><a href="../controller/deletecourse.php?id='.$course['course_id'].'"><button Onclick="return ConfirmDelete();"  type="submit">Delete</button></a></td>';
   echo '</tr>';
 }
 }
 ?>
</tbody>
</table>
<a href="adminmanagecourse.php">Click here to add new course</a>
<br/>
<br/>
<a href="#menu-toggle" class="btn btn-default" id="menu-toggle">Toggle Menu</a>
</div>

<?php

$table = "courses";
$condition=" ";
$select=Selectdata($table,$condition);
$total_records = $rowcount;
$total_pages = ceil($total_records / $per_page);

echo '<div class="text-center">';
echo '<ul class="pagination">';
echo "<li><a href='courselist.php?page=1'>".'<<'.'</a></li>';
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='courselist.php?page=".$i."'>".$i.'</a></li>';
}
echo "<li><a href='courselist.php?page=$total_pages'>".'>>'.'</a></li>';
echo "</ul>";
echo '</div>';

?>

<?php
include("../view/common/footer.php");
?>

functions.php

function Selectdata($table,$condition="")
{
    global $conn,$result,$selectarray,$rowcount;

    $sql="SELECT * from ".$table." ".$condition." ";
    $result=$conn->query($sql);
    $rowcount=$result->num_rows;
    while($row=$result->fetch_assoc())
    {   
        $selectarray[]=$row;
    }
    return $result;
}

Upvotes: 0

Views: 334

Answers (2)

Amar Shukla
Amar Shukla

Reputation: 136

Try this :

foreach($selectarray as $row)
{
  echo '<option value="'.$row['course_id'].'">'.$row['course_name'].'</option>';
}

Upvotes: 0

Nedret Recep
Nedret Recep

Reputation: 728

I hope this code is not intended to be used in a real project.

Just add

$selectarray = array();

before

while

in Selectdata function definition.

What happens is that each time you call that function it just amends the full rowset to the same array as it is global.

Upvotes: 1

Related Questions