I have a task to make table which is searching email from database table and after search result those elements and at the same time after checkbox selection can export data to csv and can delete table element, show 10 elements per page. I am new with php and don't know how to resolve this problem. If someone can realy help I can give my email or github just put + on comment.
<form method="post" action="#">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="search"><br><br>
<input type="submit" name="export" value="export CSV"> <br>
<?php
include_once ('conection.php');
include_once ('search.php');
include_once ('sort.php');
include_once ('paging.php');
if ($resultSet ->num_rows > 0){
?> <table> <?php
echo"
<tr>
<th> <a href ='?order=DayTime&&sort=$sort'> Date </th>
<th> <a href ='?order=Email&&sort=$sort'> Email </th>
<th> Delete </th>
<th> Checkbox </th>";
while($rows = mysqli_fetch_array ($result) ){
echo "
<tr>
<td>".$rows['DayTime']."</td>
<td>".$rows['Email']."</td>
<td> <a href=\"delete.php?id=".$rows['id']."\">delete</a></td>
<td> <input type='checkbox' name='checkbox[]' value='".$rows['id']."' </td>
</tr> ";
}
?> </table> <?php
} else {
echo "No records returnet."; }
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo " <a>$counter</a>";echo "<br>";
}else{
echo "<a href='?page_no=$counter'>$counter</a>";
echo "<br>";
} } }
echo $page_no." of ".$total_no_of_pages;
?>
<form action="#" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="search"><br><br>
<table border="1">
<tr>
<th> <a href ='?order=Email&&sort=$sort'>Email</th>
<th>DayTime</th>
<th> Delete </th>
<th> Checkbox </th>
</tr>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['Email'];?></td>
<td><?php echo $row['DayTime'];?></td>
<td> <a href=\"delete.php?id=".$rows['id']."\">delete</a></td>
<td> <input type='checkbox' name='checkbox[]' value='".$rows['id']."'> </td>
</tr>
<?php endwhile;?>
</table>
</form> `` and code for search is ``` if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM email WHERE Email LIKE '%".$valueToSearch."%' ";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM email";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$con = mysqli_connect("localhost","root","","email_adress");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$filter_Result = mysqli_query($con, $query);
return $filter_Result;
} ``` and code for delete is ``` include_once('conection.php');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id']; // $id is now defined
// or assuming your column is indeed an int
$id = (int)$_GET['id'];
mysqli_query($con,"DELETE FROM email WHERE id='".$id."'");
mysqli_close($con);
header("Location: sort_gatavs.php"); ``` for search code is ``` if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM email WHERE Email LIKE '%".$valueToSearch."%' ";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM email";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$con = mysqli_connect("localhost","root","","email_adress");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$filter_Result = mysqli_query($con, $query);
return $filter_Result;
} ``` and the main problem that I know for shure that is one of the problem is code which is responsable for csv exporting ``` if( !isset($_POST['xport']) OR !is_array($_POST['xport']) ) {
exit('No rows selected for export');
}
$conn = mysql_connect("localhost","root","");
mysql_select_db("email_adresse",$conn);
$filename = "peem_results.csv";
$fp = fopen('php://output', 'w');
$query = "SELECT * FROM email";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
$header[] = $row[0];
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);
//Cast all ids to integer
$ids = $_POST['xport'];
array_walk($ids, function(&$value, $key) {
$value = (int)$value;
});
$ids = implode(', ', $ids);
$query = "SELECT * FROM results WHERE id IN ($ids)";
$result = mysqli_query($query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit; ```
Aucun commentaire:
Enregistrer un commentaire