I have DataTables table with data from one mysql table and after click on table row, I need to show data from another mysql table. I'm sending the data from both tables in JSON. I have this script (jsfiddle) and this is the php script to get the data from DB:
<?php
$tic = $_POST['name'];
$conn = mysqli_connect("192.168.2.11", "admin", "pass", "mydb");
$result = mysqli_query($conn, "SELECT TIME, DESC FROM table1, table2 WHERE table1.ACTION=ACT AND ID='" . $tic . "'");
// storing in array
$data = array();
while ($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
echo json_encode($data);
// FOLLOWING CODE WORKS WITH THE CODE IN FIDDLE..
/*
echo json_encode( [
"html" => 'Details for <b>'.htmlentities($_POST['name']) . '<br><p>
] );
*/
$conn->close();
?>
This is my JSON I need to show in row details in table:
[
{"TIME":"2016-05-24 04:48:25","DESC":"Created"},
{"TIME":"2016-06-25 07:53:36","DESC":"Completion"}
]
I need to show the data from second ajax call in the format function and I need to style them to tables (not another datatable), because there will be more data in the future. I tried to modify format function and return rowData[0], but it returns data from 1st table, not the row details. Thanks
JSFIDDLE
Related
I want to display a score coming from my database through a json dataset (from the table "score"), which works fine, but my quiz_id is the foreign key, which means the dataset will contain the id, and not the name of the quiz. Which doesn't look good on the CanvasJS graph. The quiz_name is located in the quiz table, with the primary key quiz_id. How would i make the json dataset include the quiz_name instead of quiz_id?
my test.php, which is creating the json:
<?php
header('Content-Type: application/json');
$con = mysqli_connect("123.123.123.123", "Seba0702", "", "kayeetdb");
$data_points = array();
$result = mysqli_query($con, "SELECT * FROM score");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['quiz_id'] , "y" => $row['quiz_score']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>
My tables:
Quiz Table:
Score Table:
I want the json to include the quiz_name and quiz_score.
For retrieve infor from another table you need a join
SELECT score.quiz_id, score.student_id, score.quiz_score, quiz.name
FROM score
INNER JOIN quiz on quiz.quiz_id = score.quiz_id
i have a table 'class' with thier fee structure. i want to show fee amount if a particular class is selected by user.
i'm able to fetch only one value from database want ot show multiple value..
here is my db table:
here are my codes:-
fetch.js
$('#class').click(function(){
$.getJSON(
'fetch2.php',
'class=' + $('#class').val(),
function(result){
$('#tution_fee').empty();
$.each(result.result, function(){
$('#tution_fee').append('<option>'+this['tution_fee']+'</option>');
});
}
);
});
fetch.php
<?php
define('HOST','localhost');
define('USERNAME', 'root');
define('PASSWORD','');
define('DB','bethel');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$class = $_GET['class'];
$sql = "SELECT tution_fee FROM class WHERE class='$class'";
$res = mysqli_query($con,$sql);
$result = array();
while ($row = mysqli_fetch_array($res)) {
array_push($result,
array('tution_fee'=>$row[0])
);
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
first one is fetch.js and second fetch.php
here you can see the JS code that one value can be fetched from database but i want to fetch multiple value.
please help
Basically I have a database and I'm making a webpage with PHP.
I get a row of Elements from my database and put them into an array.
I need to display this array in my webpage, and the elements out of it have to be ajax links that display more information.
$sql = "SELECT * FROM categories";
$categories1 = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($categories1)) {
$id = $row["name"];
$echo "$id";
This is my array where i have a list of names and i print it there, i want my prints to be links.
Any help? I'm pretty desperate
Make the datatype of AJAX call as JSON in the javascript and return a json encoded string from php. Then in the success block of the AJAX call , parse it by JSON.parse()
$sql = "SELECT * FROM categories";
$categories1 = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($categories1)) {
$categoryDetails[] = $row;
}
echo json_encode( $categoryDetails );
I am trying to fill a table using a jquery .get request, the url being a php file. The php gets data from a database, then it should return a json array back to the jquery, array which will fill the table. While the length of the array is returned as it should, the table cells are empty.
Here is my get.php function:
<?php
$mysqli=new mysqli("localhost:3306","root","","leagues");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$return_arr = array();
$query = "SELECT * FROM league";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_row())
{
$return_arr[] = $row;
}
}
$mysqli->close();
header('Content-Type: application/json');
echo json_encode($return_arr);
?>
And here is my jquery get function
$.get('php/get.php',function(responseJson)
{
if(responseJson!=null)
{
$("#tableleague").find("tr:gt(0)").remove();
var table1 = $("#tableleague");
$.each(responseJson, function(key,value) {
var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(value['teams']);
rowNew.children().eq(1).text(value['playedgames']);
rowNew.children().eq(2).text(value['wongames']);
rowNew.children().eq(3).text(value['tiegames']);
rowNew.children().eq(4).text(value['lostgames']);
rowNew.children().eq(5).text(value['scoredgoal']);
rowNew.children().eq(6).text(value['receivedgoal']);
rowNew.children().eq(7).text(value['points']);
rowNew.appendTo(table1);
});
}
});
Here is how my webpage looks, with the php file response shown.
Since the response is ok, what am I doing wrong that the cells aren't filled? Thank you.
If you look at your JSON data, you can see that there are no keys such as teams or playedgames. This is because you used fetch_row() in the PHP. Change that to fetch_assoc():
while ($row = $result->fetch_assoc())
This will give you $row with the field names as keys instead of using numerical keys that fetch_row() provides.
You can turn the php json into javascript object
obj = JSON.parse(json);
es:
var json='{"ex1":"test","ex2":[{"sub1":"test"},{"sub2":""s2test}],"ex3":true}';
var obj = JSON.parse(json);
after you can acces to data with :
obj.ex1 // test
obj.ex2[0].sub2 //s2test
I have a scenario like this:
I will take a number as input and will store in the database along with other information. And will display the data from the database as per the number position that have been taken as input. For example: if the input is 4, the display order will be in fourth position.
I want to make this using PHP/Mysql.
Thanks for the suggestions...
suppose you have field serial_number in your table iserted for serial by input, then firstlly you have to fetch table in array and then sort the table according to the serial number the code is like follow
<?php
$con=mysql_connect("localhost","root","");
$sql="select * from sort.data";
$result=mysql_query($sql,$con);
$data=array();
while($row=mysql_fetch_assoc($result))
{
$data[]=$row;
}
echo '<br />content of data before sorting';
echo '<pre>';
print_r($data);
echo '</pre>';
$final = array();
foreach($data as $key=>$item)
{
for($i=0;$i<$key;$i++)
{
if($data[$i]['serial_number']>$data[$key]['serial_number'])
{
$temp = $data[$key];
$data[$key] = $data[$i];
$data[$i] = $temp;
}
}
}
echo '<br />content of data after sorting';
echo '<pre>';
print_r($data);
echo '</pre>';
?>
here lastly $data containing the sorted array as per your requirement then you applay the table and td on it for formatting
as Saurabh Sinha commented. I am describing his comment.
store your input value in table such as position
$position = $_REQUEST['position']; // position submit by your form eg:4
insert record with position
mysql_query("INSERT INTO `your_table`(field1, field2,....,position) VALUES('$field1_value', '$field2_value',.....,'$position')");
now when ever you retrive your records and be sort according to the position. using ORDER BY like this
$sql = "SELECT * FROM `your_table` WHERE [YOUR CONDITIONS] ORDER BY position [DESC/ASC]";
in [] optional.
eg : if you have no condition to apply and want to order in Descending use given query
$sql = "SELECT * FROM `your_table` ORDER BY position DESC";