get a particular column element of the next table row from database - php

I have a query which selects userid,messageid,statusid from tableA like following.
$qry = mssql_query('select userid,messageid,statusid from tableA');
if(mssql_num_rows($qry))
{
$data = mssql_fetch_array($qry)
{
//if(current_status_id column value != next_status_id column value)
$status = $data['statusid'];
}
}
I need to compare the value of current statusid column with the immediate next row statusid column like this if(current_status_id column value != next_status_id column value).Is this possible.Pls help me

$qry = mssql_query('select userid,messageid,statusid from tableA order by statusid');
while (($row=mssql_fetch_array($qry) !== FALSE) {
if (isset($previous) && $previous['statusid'] !== $row['statusid']) {
// do what you gotta do here
}
$previous = $row;
}
I added order by statusid to your SQL so that you do get an order set of data. And rather than trying to "look ahead" to the next row, the code above "looks back" to the previous row ... which is effectively the same. You've got the data of the two rows in $previous and $row so you should be able to do what you wanna do with $previous.

I have created an array and assigned the results into it.Then I used while loop and checked if the current element is not equal to next element.
$i=1;
while (($row = mssql_fetch_array($qry, MYSQL_ASSOC)) !== false)
{
$data_array[] = $row; // add the row in to the results (data) array
}
mssql_data_seek( $qry, 0 );
while($msgDetailChilds = mssql_fetch_array($qry)){
if($data_array[$i]['groupid']!=$data_array[$i-1]['groupid'])
{
//do stuff
}
}

Related

How to check all values present in the column 'pincode'?

This code only reads the first value present in the column. If the value posted in the html form matches the first value, it inserts into the database. But I want to check all the values in the column and then take the respective actions.
For example, if i give input for 'ppincode' and 'dpincode' as 400001, it accepts. but if i gave 400002, 400003,..... it displays the alert even if those value are present in the database
DATABASE:
pincode <== column_name
400001 <== value
400002
400003
400004
...
also i tried this
$query = "SELECT * FROM pincodes";
$result = mysqli_query($db, $query);
$pincodearray = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
$pincodearray[] = $row;
}
}
If I understand well - you want to compare value from POST request with all retrieved records saved in DB and if it matches - perform action.
If so, I would recommend using for(each) loop. Example:
if( !empty($row){
foreach( $row as $key ){
if($key['pincode'] == $ppincode && $key['pincode'] == $dpincode){
// your action goes here
}
}
}
Additional tip: use prepared statements :)
SELECT count(*) FROM table WHERE ppincode=ppincode AND bpincode=bpincode
if this return 0 then insert or else show alert.

Updating Rows in Table on While loop using MySQL/PHP

Can anybody tell me what I'm doing wrong i want to update my Mysql table with 3 rows. I Selecting checkboxes and i press button then nothing happend with upadate of table. It's saving good id's to array, but update query doesen't work.
$s = mysql_query("SELECT data.id, data.sonda_data, data.type, odp.id, odp.sonda_data, odp.type, odp.wyniki, odp.idp FROM data,odp WHERE data.id=odp.idp");
$i=0;
$tab= array();
while ($row = mysql_fetch_row($s))
{
if ( (isset($_POST['pole'.$row[3]])) && (!isset($_SESSION['security'])) )
{
$id = $_POST['pole'.$row[3]]; // get id from radio boxes
$tab[i]=$id; // saving radio boxes id's to array
$i++; // increment array
// array look like example 8,11,10
if($i==3)
{
while($i>=1)
{
mysql_query("UPDATE odp SET wyniki = (wyniki + 1) WHERE 'id=$tab[$i]'");
$i=$i-1;
}
$_SESSION['security'] = true;
}
}
}

Finding a match between array and database

I have an array called "selected_checkboxes". In my database I have multiple columns and some of them contain the value 1, otherwise the value will be NULL. The names of those columns are exactly the same as the values of the array.
I would now like to check dynamically if the values of my array ($_POST['selected_checkboxes']) match with the values of my columns in the database. If ALL values from the array have the value 1 in the database, it should echo something like Match!
This is what I have tried so far but I think it's completely wrong:
if(!$result = $db->query("SELECT * FROM products")){
die('Error');
}
while($row = $result->fetch_object()){
foreach($_POST['selected_checkboxes'] as $value) {
if ($value == 1) {
$say .= $value . ' is = 1';
}
}
}
echo $say;
I appreciate any help!!
Edit:
This is how the array 'selected_checkboxes' is getting generated:
$('.show_result').click(function() {
// Get all selected checkboxes
var selected = [];
$.each($("input[type=checkbox]:checked"), function(){
selected.push($(this).attr('id'));
});
// Get the selected option field
selected.push($('#PG_Select_Bereich1').val());
// Deliver data to PHP
$.ajax({
url : "typo3conf/ext/produkteguide_sg/pi1/products.php",
type: "POST",
data : { selected_checkboxes:selected },
success: function(data, textStatus, jqXHR)
{
$("#PG_Resultate").html(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
//alert(errorThrown);
}
});
And this is how my database looks like:
I've done something similar but in a different way, I hope this is what you're looking for:
// Query execution
$result = mysqli_query($CONN,"SELECT * FROM whatever");
while($row_from_db = mysqli_fetch_array($result)){
$db_value = is_null($row_from_db['myValue']) ? 0 : 1;
$check_value = !isset($_POST['selected_checkboxes']['myValue']) ? 0 : 1
echo $db_value == $check_value ? "Match!" : "Not matching :(";
}
I am writing pseudo code for your requirement.
1) loop through array.
2) while loop through array if value of element is found 1 the go to next step otherwise continue loop.
3) If array element value is 1 then get a key of element and prepare sql query which checks that column name same as key name have value as 1 for each record then mark it as Match.
Hope this helps to you. If you not get with this then let me know i will write the code for you.
Edit:
Each of your checkbox name must be same as column name in database.
$array_checkbox = $_POST['selected_checkboxes'];
$query =" SELECT count(*) FROM <tabel_name> WHERE 1=1";
// get count from query and store it in $total_rows variable
$total_rows = 10; // for example purpose we take count value as 10
foreach($array_checkbox as $key => $checkbox){
$query = $query =" SELECT count(*) FROM <tabel_name> WHERE $key=1"; // here we take key of array as column name in sql query and getting that how many rows of the column have value 1
// get count from query and store it in $result variable
$result = 10;
if($result == $total_rows){
echo "Match";
}
}
Hope this is according to your requirement.

Displaying whole table with optionally empty fields

Here is how the database looks like
So I would like to display it like
Champion name
name of the column e.g. Q name of the spell - Surging Tides
the rest of spells for that champion
Next Champion etc.,
This is the way I display Champion names right now
$champions = $conn->prepare("SELECT *
FROM champions
Where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
$champions_result = $champions->get_result();
while($row = $champions_result->fetch_assoc()){
echo $row['Champion'].' '.$row['NumNotNull'].'<br>';
}
I can't really think of an easy way to do this with the least amount of queries possible.
Here is another example how it should look like
$row is an associative array, so you can loop through it with foreach and test whether the column is empty.
while($row = $champions_result->fetch_assoc()){
echo $row['Champion'].' '.$row['NumNotNull'].'<br>';
foreach ($row as $column_name => $column) {
if ($column_name == 'Champion' || $column_name == 'NumNotNull') {
continue; // These fields were already displayed above
}
if (!empty($column)) {
echo "$column_name $column<br>";
}
}
}

PHP Selectbox function not working quite right?

I have a table teamtrack_activity that holds id and activity_name.
I have table teamtrack_entry that holds all team daily entries. This table has a field "activity_id" that I want to store the teamtrack_activity id. I have this part working.
However I am having 2 issues:
Displaying activity_name instead of id. When I try to do this then activity_name gets passed and this of course doesn't work.
When I go back to edit the entry it does not show the value in the database. It just shows the select box anew.
if ($key == "activity_id")
{
$query="SELECT id, activity_name FROM teamtrack_activity Order By id";
$res = sql_query($query);
if ($res === FALSE)
{
trigger_error(sql_error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
$select_options["entry.$key"] = array();
for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
{
$select_options["entry.$key"][$row['id']] = $row['id'];
}
}
Change:
$select_options["entry.$key"][$row['id']] = $row['id'];
into
$select_options["entry.$key"][$row['id']] = $row['activity_name'];
to get a dropdown with the activity ame, which submit the activity ID for your script.
Based on your comment;
It might be the parsing of your $select_options.
In a foreach loop you would want to
foreach($select_options["entry.$key"]) as $key => $label)
{
echo "<option value=".$key.">".$label."</option>";
}

Categories