joined table display without duplicate - javascript

I'm trying to get data from database using PHP this the Sql request you'll see that I have 3 tables but in table Milestoneevent I have various values for column libelle so I want to display like this
Id num libelle1 libelle2 libelle3 ..... sql reql request and php
select DISTINCT file.num,file.id as filenumber,file.numlta,milestones.id,milestones.libelle,milestoneevent.idmilestone,milestoneevent.dat,milestoneevent.idfile from file,milestones,milestoneevent where milestoneevent.idfile=FILE.num and milestoneevent.idmilestone=milestones.id
while (row=mysqlfetchassoc(
rs_result)) {
//print_r( $row ); // debug code ?>
<tr>
<td><input type='checkbox' name="approve[]" id="check" value=<?php echo $row['num']?>></td>
<td><?php echo $row['filenumber']; ?></td>
<td><?php echo $row['numlta']; ?></td>
<td><?php echo $row['designation']; ?></td>
<td><?php echo $row['libelle']; ?></td>
<td><?php echo $row['milestonedate']; ?></td>
</tr>
in the picture you see that a row cqn have multiple values for column libelle in different dates
edit
As requested in comments, here is my expected output:
I was forced to add picture here cause in comment discussion can not add it

You can use GROUP_CONCAT, in your query:
select...,GROUP_CONCAT(milestones.libelle SEPARATOR ';'),..FROM....WHERE...GROUP BY milestones.id

Related

How to add a column specific with values depending on other cell's value using javascript or php?

Let's say for example column dropdown represents a type of membership and depending on the value of dropdown, I would like to be able to display a column next to it that indicates the maximum number of companions I could bring. Is there a way to do it via javascript or PHP or maybe SQL itself?
<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['name']?></td>
<td><?php echo $rows['email']?></td>
<td><?php echo $rows['number']?></td>
<td><?php echo $rows['org']?></td>
<td><?php echo $rows['dropdown']?></td>
<td><?php echo $rows['date']?></td>
</tr>
<?php
}
?>
Code:
<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td id="<?=$rows['id']?>"><?php echo $rows['name']?></td>
</tr>
<?php
}
?>
By clicking on the ajax button you will take the ID attribute of the column, send a request with the received id and add the value to the column created next to it.
If I understand you correctly.

Exporting php data in a loop to CSV file

I have written a code which uses the API of fantasypremierleague.com to extract data. The data is being extracted for 38 gameweeks using a for loop. I print the data using echo to verify that it is correct.
I am having a problem converting the data into CSV files using a script since its in a loop.
After printing information each gameweek i have added a button (in total 38 buttons) which should convert the information of each gameweek to a CSV file.
Currently, all the buttons are printing the information of gameweek 1 even though the script is part of the loop.
ini_set('max_execution_time', 300);
for ($i=1; $i <39 ; $i++) {
$json=file_get_contents("https://fantasy.premierleague.com/drf/entry/1224012/event/".$i."/picks");
$data = json_decode($json, true);
$json1=file_get_contents("https://fantasy.premierleague.com/drf/elements/");
$data1 = json_decode($json1, true);
?>
<table id ="gameweek_history">
<tr>
<th>Average Gameweek Score</th>
<th>Highest Score</th>
<th>Highest Scorer ID</th>
</tr>
</br>
</br>
<td><?PHP echo $data['event']['average_entry_score']; ?></td>
<td><?PHP echo $data['event']['highest_score']; ?></td>
<td><?PHP echo $data['event']['highest_scoring_entry']; ?></td>
</table>
<table id ="gameweek_info">
<tr>
<th>Gameweek</th>
<th>Active Chip</th>
<th>ID</th>
<th>Points</th>
<th>Total Points</th>
<th>Rank</th>
<th>Overall Rank</th>
<th>Gameweek Transfers</th>
<th>Gameweek Transfer Cost</th>
<th>Points on Bench</th>
<th>Bank</th>
</tr>
</br>
</br>
<td><?PHP echo $data['entry_history']['event']; ?></td>
<td><?PHP echo $data['active_chip']; ?></td>
<td><?PHP echo $data['entry_history']['id']; ?></td>
<td><?PHP echo $data['entry_history']['points']; ?></td>
<td><?PHP echo $data['entry_history']['total_points']; ?></td>
<td><?PHP echo $data['entry_history']['rank']; ?></td>
<td><?PHP echo $data['entry_history']['overall_rank']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers_cost']; ?></td>
<td><?PHP echo $data['entry_history']['points_on_bench']; ?></td>
<td><?PHP echo $data['entry_history']['bank']; ?></td>
</table>
</br>
</br>
</br>
<script>function doCSV() {
var table1 = document.getElementById("gameweek_history").innerHTML;
var data1 = table1.replace(/<thead>/g, '')
.replace(/<\/thead>/g, '')
.replace(/<tbody>/g, '')
.replace(/<\/tbody>/g, '')
.replace(/<tr>/g, '')
.replace(/<\/tr>/g, '\r\n')
.replace(/<th>/g, '')
.replace(/<\/th>/g, ',')
.replace(/<td>/g, '')
.replace(/<\/td>/g, ',')
.replace(/\t/g, '')
.replace(/\n/g, '');
var table2 = document.getElementById("gameweek_info").innerHTML;
var data2 = table2.replace(/<thead>/g, '')
.replace(/<\/thead>/g, '')
.replace(/<tbody>/g, '')
.replace(/<\/tbody>/g, '')
.replace(/<tr>/g, '')
.replace(/<\/tr>/g, '\r\n')
.replace(/<th>/g, '')
.replace(/<\/th>/g, ',')
.replace(/<td>/g, '')
.replace(/<\/td>/g, ',')
.replace(/\t/g, '')
.replace(/\n/g, '');
var data= data1.concat(data2);
var mylink = document.createElement('a');
mylink.download = "Gameweek.csv";
mylink.href = "data:application/csv," + escape(data);
mylink.click();
}</script>
<button onclick="doCSV()">Export HTML Table To CSV File</button>
<?PHP
}
?>
I expect each button to convert the information of the particular gameweek which the loop is currently on (for e.g. i i=1 then gameweek 1 team should be exported, if i=2 then gameweek2 team should be exported)
You'll need to add unique ids to every table like:
<table id ="gameweek_history<? echo $i; ?>">
Then you'll need to have your javascript refer to the id. Your existing code creates a new script for every table, so to keep doing that, each function will need a unique name that has refers to that table.
<script>function doCSV<? echo $i; ?>() {
var table1 = document.getElementById("gameweek_history<? echo $i; ?>").innerHTML;
It would probably be better to just have one function declared outside the loop with a variable passed into it.
<script>function doCSV(table_number) {
var table1 = document.getElementById("gameweek_history" + table_number).innerHTML;
Then add the table number to each event handler.
<button onclick="doCSV(<? echo $i; ?>)">Export HTML Table To CSV File</button>

Delete from database using javascript

I have a page containing a database table with all the rows and columns.
What I am trying to do is to select all the rows I want and then delete them when I click on the button.
This is what I've done so far in the table.php page:
<?php
include "config.php"; //connection to database
incude "home.js";
$funcao="Select * from palavras";
$result=mysqli_query($link, $funcao);
?>
<button id="button_apaga" type="button" onclick="delete()" > DELETE </button>
<?php if($result->num_rows > 0) { ?>
<table class="table">
<tr>
<th>IdPalavra</th>
<th>Palavra</th>
<th>Grau de Dificuldade</th>
<th>Data</th>
<th>Hora</th>
<th>Selecionar</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr role="row">
<td><?php echo $row['idpalavras']; ?></td>
<td><?php echo $row['palavra']; ?></td>
<td><?php echo $row['graudificuldade']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['hora']; ?></td>
<td><input type="checkbox" name="check" id="checkbox" /></td>
</tr>
<?php } ?>
</table>
<?php }
else{
echo "0 resultados";
} ?>
JavaScript Page (home.js):
function delete(id){
var check = document.getElementById('checkbox');
if(check.checked) {
// sql query
}
My question is how can I do que sql query considering it's in a different page. Can I just open php and put the query inside?
Also how can I receive all the IDs from the selected rows to the function?
Thank you in advance.
One approach would be to use AJAX. For the purpose of condensing the code, I'm going to also incorporate jQuery into this solution. I am also going to change your checkbox to an individual button link for the sake of making this a bit less code. A solution to delete multiple at the same time could work similarly to this, but since you're using AJAX most likely this is going to be easier for your users.
Modify table.php
<?php
include "config.php"; //connection to database
incude "home.js";
$funcao="Select * from palavras";
$result=mysqli_query($link, $funcao);
?>
<?php if($result->num_rows > 0) { ?>
<table class="table">
<tr>
<th>IdPalavra</th>
<th>Palavra</th>
<th>Grau de Dificuldade</th>
<th>Data</th>
<th>Hora</th>
<th>Selecionar</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr role="row" class="palavras_row">
<td><?php echo $row['idpalavras']; ?></td>
<td><?php echo $row['palavra']; ?></td>
<td><?php echo $row['graudificuldade']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['hora']; ?></td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js?ver=3.3.1"></script>
<script type="text/javascript">
(function($) {
$(document).on('click', '.palavras_row a.palavras_delete', function() {
var _id = $(this).attr('data-id');
var _row = $(this).parent().parent();
$.ajax({
url: 'delete.php',
data: {
idpalavras: _id
},
type: 'POST',
dataType: 'json',
success: function(__resp) {
if (__resp.success) {
_row.remove(); // Deletes the row from the table
}
}
});
});
})(jQuery);
</script>
Create a new file in the same folder as your table.php and name it delete.php
<?php
$idpalavras = filter_input(INPUT_POST, 'idpalavras', FILTER_SANITIZE_NUMBER_INT);
$success = false;
if ($idpalavras) {
include "config.php"; //connection to database
$funcao="delete from palavras where idpalavras = " . $idpalavras;
$result=mysqli_query($link, $funcao);
$success = true;
}
header('Content-Type: application/json');
echo json_encode(array('success' => $success));
The solution above sends a simple command to your PHP backend where the delete query can be run by PHP. You cannot run a mysql command directly from javascript since that is frontend code.
This code is a functional solution, but it is abbreviated; a more complete solution would have more detailed handling of potential errors (either via AJAX or processing your delete query). It should also have some security on your delete.php to make sure unauthorized users aren't able to delete records without the proper permission to do so.

displaying data from the database that can be removed from the php page with a checkbox using ajax,

guys, I am doing a project in PHP where I should display in a page data from the database and remove from the page(not database) if I checkbox them using ajax. any source or link that could help me understand better this? thank you!!
p.s all I've done so far is deleting the data from the page and database at the same time
while($row = mysqli_fetch_array($result))
{
<tr id="<?php echo $row["id"]; ?>" >
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["surname"]; ?></td>
<td><input type="checkbox" name="id[]" class="delete" value="<?php echo $row["id"]; ?>" /></td>
**deleting method **
$.ajax({
url:'delete.php',
method:'POST',
data:{id:id},
success:function() {
for(var i=0; i<id.length; i++){
$('tr#'+id[i]+'').css('background-color', '#ccc');
$('tr#'+id[i]+'').fadeOut('slow');
If you're not deleting from the database then using AJAX isn't needed at all; use a javascript event listener that is triggered by a "delete selected" button or by the checkboxes themselves to remove the row from the table

Dynamically adding and deleting table rows - Yii Framework

I am trying to create form by dynamically adding rows and deleting it when user clicks on delete button using php code,
below is my code to render first row while opening the form ,
<div class="selector-details" style="display:none">
<div class='newfield'>
<div id='container'>
<table id="tid">
<tr>
<td><?php echo CHtml::dropDownList('field_list','',$field_name); ?></td>
<td><?php echo CHtml::dropDownList('field_list','',$operator); ?></td>
<td><?php echo CHtml::textField('querybox'); ?></td>
<td> <?php echo CHtml::imageButton(Yii::app()->request->baseUrl.'/images/Trash.jpg',array('class'=>'trash-action')); ?></td>
<?php echo "<br>"; ?>
<td> <?php echo CHtml::dropDownList('condition_check','',$condition_check);?></td>
</tr>
</table>
</div>
</div>
<?php
echo CHtml::button('Add',array('class'=>'addfield-button','background-style'=>'none'));
how i should make call the above code to add rows and delete particular row when user clicks on the row delete button ? I am new to yii please provide any idea to go further.
$script = 'alert("hello")';
Yii::app()->getClientScript()->registerScript('#test', $script,CClientScript::POS_HEAD );
echo CHtml::button('Add',array('class'=>'addfield-button','background-style'=>'none','onclick'=>$script));
if you real want to use Yii style, you can open framework code and there is a good code that you want, search : CButtonColumn.php

Categories

Resources