I am using flexigrid for displaying data and need help to check a row for a value and change it. The data is being returned to flexigrid via json and works well and without ant errors. however, what I am trying to do is check the row; $row['status'] for a value = 1 and if that row is that value then change it to value = "In".
I have tried various ways to code using an if statement, but it does not error, just dosen't display the data in the grid. What is the correct way to code this row? Thanks
$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
$data['rows'][] = array(
'id' => $row['Id'],
'cell' => array($row['Id'], $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay'], $row['customer'], $dropdown, $service, $row['department'], if ($row['status']==1){$row['status']="In"}, $row['custref'], $row['size'], $row['authorisation'], date('d/m/Y H:m:s',strtotime($row['intake_date'])), date('d/m/Y',strtotime($row['destroy_date']))));
}
echo $json->encode($data);
Related
I'm having trouble iterating through a PHP array in order to display a chart. Right now, my code is only resulting in the display of one column in the chart (this column is displaying correctly), but I can't seem to get other columns to display.
This is my code right now (in a php section at the top of my html page). I know that the issue is with this section of code, because the chart is rendering perfectly, but just not adding a column for each record in the table.
I'd really appreciate any insight into the mistakes I'm making here. Thank you.
$valueAnimalType = $_POST['animaltype'];
$connect = mysqli_connect("127.0.0.1","____","_____",3306);
$result = mysqli_query($connect,"SELECT * FROM DISPOSAL");
$datas = array();
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
foreach ($datas as $data){
$datas = array(
array('y' => $data[$valueAnimalType], "label" => $data['DisposalName'] ));
}
}
modify your code here:
foreach ($datas as $data) {
//LINE BELOW
$datas [] =
array('y' => $data[$valueAnimalType], "label" => $data['DisposalName']);
//LINE ABOVE
}
you're overwriting your $datas each time loop passes with last record, now it's appending new item to array
I've been trying to figure out how to load the results of a search into a table but no matter what I do, I'm only getting one single result instead of the 2 (sample size) rows that I have in the table in the db.
This is the MySQL Code:
if (isset($_POST['search_value'])) {
$search_value = mysqli_real_escape_string($conn, $_POST['search_value']);
$sql = "SELECT
record_id,
personal_id,
name,
status,
entry_date
FROM sample_db
WHERE EmpID = '".$search_value."'";
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));
$data = array();
while($row = mysqli_fetch_array($res)){
$data = array(
'tb_record_id' => $row['record_id'],
'tb_personal_id' => $row['personal_id'],
'tb_name' => $row['name'],
'tb_status' => $row['status'],
'tb_entry_date' => $row['entry_date'],
);
}
echo json_encode($data);
}
I've read several examples where the array is built as $data[] = array( data goes here) instead of $data = array(data goes here) but whenever I try $data[], it doesn't return anything to the table BUT the console log does show all the results within the array.
also, using dataType: 'json' doesn't seem to work either.
The only way I've tried this so far is by giving an id to each <td>.
ajax code
$.ajax({
type : 'POST',
url : 'search_fetch.php',
data : data,
cache: false,
success : function(response)
{
result = jQuery.parseJSON(response);
$("#list_p_id").append(result.tb_personal_id);
$("#list_name").append(result.tb_name);
$("#list_status").append(result.tb_status);
$("#list_date").append(result.tb_entry_date);
}
});
How can I populate the table with all available results?
Also in case if helps, I am open to not use an array. I just don't know another way of how to send the results of a query to an ajax response.
In your while loop, you need to push each row of results onto your array in a way that doesn't overwrite the entire array each time.
For example:
while($row = mysqli_fetch_array($res))
{
array_push($data, array(
'tb_record_id' => $row['record_id'],
'tb_personal_id' => $row['personal_id'],
'tb_name' => $row['name'],
'tb_status' => $row['status'],
'tb_entry_date' => $row['entry_date'],
)
);
}
When you do this, keep in mind that $data is now an array of arrays, and you will need to access the items accordingly in your ajax.
ADDITIONAL INFORMATION TO ANSWER YOUR QUESTION:
I didn't realize you also need information about how to display the resulting data in your table...
You are going to need to use some kind of dynamic code to product your HTML in order to assign each table cell with a unique id.
When you have done that, you will need to use a loop in your ajax code so you can assign the resulting data to each table cell and display it.
Currently your loop will just overwrite the data into the existing elements over and over, meaning you only get one row of information.
If i create an array from results returned in a mysqli query is there a way to select and use only one specific row from the array?
$info= array();
while($row = mysqli_fetch_assoc($query)) {
$info[] = array(
'id' => $row['id'],
'location' => $row['location']
);
}
How would i go about displaying only a single row from this array where the id equals a variable like $id?
In your loop you could just do something like:
if ($id == $row['id']) {
$info[] = $row;
}
However it would make more sense to me to just update your query.
SELECT cols FROM t1 WHERE id = :id
Using $id as a parameter.
I am basically trying to fetch results from a SQL database and load that into a multidimensional array so i can use them later on in the page.
while($row = mysqli_fetch_array($result))
{
$send = array
(
array($row['Name'],$row['Email'],$row['Mobile'])
);
$count = $count + 1;
}
That is what i am using to get the results which if i print within the while loop it will echo all the results. However when putting it into the array it loads each result into the array as the first result. My initial plan was to use a counting variable to set where in the array the result was set to with this adding by one each time. I am not certain how to specify where to add the result i thought something along the lines of
$send = array[$count]
(
array.....
so i could then refer to the results as 0 to count length but i am not sure how to make this work. Or ,which i presume, if there is a much easier and better way of going about it. I am also not sure if this is necessary as surely the results seem to be in an array when gathered from the SQL database but i am unsure if this array is populated with each while loop or stored and can be accessed at any point
If any one can give me an example of something similar or point me at some documentation much appreciated
Try this:
$count = 0;
while ($row = mysqli_fetch_array($result)) {
$send[$count] = array($row['Name'], $row['Email'], $row['Mobile']);
$count++;
}
I have a better way for you. You could also use the id for your index, if you have one:
while ($row = mysqli_fetch_array($result)) {
$send[$row['id']] = array(
"Name" => $row['Name'],
"Email" => $row['Email'],
"Mobile" => $row['Mobile']
);
}
You can use:
$count = 0;
while($row = mysqli_fetch_array($result))
{
$send[$count] = $row;
$count ++;
}
Also you might want to use the table id as an array index, so you can access the records by ID later. In that case you can do:
while($row = mysqli_fetch_array($result))
{
$send[$row['id']] = $row;
}
You're declaring your array inside your loop. So it will reset it every time.
$send = array();
while($row = mysqli_fetch_array($result))
{
$send[] = array($row['Name'],$row['Email'],$row['Mobile']);
}
I always show my data look like bellow for printing JSON .
Current Code:
if (mysql_num_rows($data) > 0) {
while($row = mysql_fetch_array($data)) {
$info[] = array(
'ID' =>"".$row['id'],
'game_id' =>"".$row['game_id'],
'appliance_name'=>"".$row['appliance_name'],
'area'=>"".$row['area'],
'type' =>"".$row['type'],
'description'=>"".$row['description'],
'code'=>"".$row['code']
);
}
}
Here i want to show my array key name as database table column name as a example
when my query face data and store it in $info array that time automatically name of key make will be column name code'=> then the value.
So I have no tension how many column exist in database and what will be every array key name
while($row = mysql_fetch_assoc($result)) {
$info[] =$row;
}
working code.