hi i have an list of countries on table so i am sorting with country name then i get A-Z locations like this Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands,USA, Yemen , Zambiabut actually i need USA on top (i mean first element) like this order USA,Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands, Yemen , Zambiahere is my code
<?php
require 'dbconnect.php';
$empty=array();
$c=0;
$query="select * from phpfox_country";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
$message=array("country_iso"=>$row[0],"name"=>$row[1])
}
else
{
$message[$c]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
$c++;
}
$message=($message==null)?$empty:$message;
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
thanks for advance.
Why not just do with mysql query
SELECT * FROM phpfox_country
ORDER BY IF (countryName !='USA',countryName,'' ) ASC
Looks like there are some errors in your code. First, by setting $c to 0 by default and using that as an array index you are assigning the alphabetically first country to that slot, not USA. Worse still, when you actually reach the row with USA on it, you don't add it to the existing array, instead you overwrite the array by assigning a new array.
I suggest that you don't use $c at all. Instead start with an empty array, add USA to the front of the array, and all other countries to the end of the array. As for arranging the records, that is easiest to do with the SQL query. The below code assumes the column with the country name is called name - if it is not, you can fix this yourself.
<?php
require 'dbconnect.php';
// Create empty array
$message=array();
$query="SELECT * FROM phpfox_country ORDER BY name ASC";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
// Add to start of the array
array_unshift($message, array("country_iso"=>$row[0],"name"=>$row[1]));
}
else
{
// Add to end of the array
$message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
One way is to reserve a place in the beginning of the array:
<?php
require 'dbconnect.php';
$message=array(0 => 'placeholder');
$query="select * from phpfox_country";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
$message[0]=array("country_iso"=>$row[0],"name"=>$row[1])
}
else
{
$message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
$c++;
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
Modify your query and code like this:
<?php
require 'dbconnect.php';
$query = "select * from phpfox_country order by <country_name>='USA' DESC,<country_name>";
$result=mysql_query($query);
if($result)
{
$message = array();
while( false!==($row=mysql_fetch_row($result)) )
{
$message[] = array("country_iso"=>$row[0],"name"=>$row[1])
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
But not forget to replace <country_name> with country's name column
Related
This is the 1st part of the code, plz look at the variable $Tno.
if(isset($_POST['submit']))
{
$Tno=$_POST['Tno'];
$query="SELECT * FROM docs WHERE Tno='$Tno'";
$result=mysql_query($query) or die('Data not selected');
while ($row = mysql_fetch_array($result))
{
$Tassign = $row["Tassign"];
}
}
if(isset($_POST['submit2']))
{
$ti="INSERT INTO approval(Tno, approval)values('$Tno','Approved')";
$tj=mysql_query($ti) or ('Data Not Inserted');
if($tj){
echo "All set!!!!!!!!!!!!!!!!!!!!!";
}
}
Now the problem is, I need $Tno value in this part of the code also to insert in the DB. But its storing ZERO. Any solutions??
You can do this in the following ways
1)Session
You can store data in the session and than you can use it to insert the data in the database
session_start();
if(isset($_POST['submit']))
{
$Tno=$_POST['Tno'];
$_SESSION['Tno'] = $Tno
$query="SELECT * FROM docs WHERE Tno='$Tno'";
$result=mysql_query($query) or die('Data not selected');
while ($row = mysql_fetch_array($result))
{
$Tassign = $row["Tassign"];
}
}
if(isset($_POST['submit2']))
{
$Tno = $_SESSION['Tno'];
$ti="INSERT INTO approval(Tno, approval)values('$Tno','Approved')";
$tj=mysql_query($ti) or ('Data Not Inserted');
if($tj){
echo "All set!!!!!!!!!!!!!!!!!!!!!";
}
}
2) By using GET Method
You can also use $_GET method to get the value of Tno and than you can insert into your database
if(isset($_POST['submit2']))
{
$Tno = $_GET['Tno'];
$ti="INSERT INTO approval(Tno, approval)values('$Tno','Approved')";
$tj=mysql_query($ti) or ('Data Not Inserted');
if($tj){
echo "All set!!!!!!!!!!!!!!!!!!!!!";
}
}
The problem is with the statement
$query="SELECT * FROM docs WHERE Tno='$Tno'";
instead use
$query="SELECT * FROM docs";
while ($row = mysql_fetch_array($result))
{
$Tassign=$row["Tassign"];
*// Now perform the matching*
**If ($Tassign==$Tno)**
{
*// enter your functionality here
}
}
Now the values would be fetched from database and You can perform specific operation on the data fetched
Well there are 2 ways you could go about it.
Either using GET, or Session. I don't know how you're directing over from Page 1 to 2. So I will just rather use Session to be safe.
if(isset($_POST['submit']))
{
$Tno=$_POST['Tno'];
// Store the TNO value in the session
$_SESSION['Tno'] = $Tno;
$query="SELECT * FROM docs WHERE Tno='$Tno'";
$result=mysql_query($query) or die('Data not selected');
while ($row = mysql_fetch_array($result))
{
$Tassign = $row["Tassign"];
}
}
if(isset($_POST['submit2']))
{
// Now get the TNO value here
$Tno = $_SESSION['Tno'];
$ti="INSERT INTO approval(Tno, approval)values('$Tno;,'Approved')";
$tj=mysql_query($ti) or ('Data Not Inserted');
if($tj){
echo "All set!!!!!!!!!!!!!!!!!!!!!";
}
}
This code should return an array containing data from a WAMP server containing some entries about followers. Unfortunately it doesn't. Any help please?
<?php
include('ConnectionManager.php');
function getfollowers()
{
print "check";
//String query
$query="SELECT * FROM `followers` order by 'followerid'";
//execute query
$result=mysql_query($query);
//3mel array isma followers
$followers=array();
//jeeb number of rows
if(mysql_num_rows($result))
{
while($followers=mysql_fetch_assoc($result))
{
$followers[]=$followers;
}
return $followers;
}
}
?>
You're re-declaring $followers inside your loop. Change it to something else
while($row = mysql_fetch_assoc($result)) {
$followers[]=$row;
}
your issue is:
$followers[]=$followers;
you are overriding it.
while($row=mysql_fetch_assoc($result))
{
$followers[]=$row;
}
should fix the issue
I can't insert my data in mysql when count value in column No_KP when row No_KP is empty
<?php
session_start();
include ("../Connections/connection_db.php");
include ("../Connections/db_connect.php");
$no_kp = '765454104321';
$id = '101';
mysql_select_db($database_connection_db, $connection_db);
$query_viewAduan = mysql_query("SELECT No_KP, COUNT(*) FROM aduan_tidak_hadir WHERE No_KP LIKE '%".$no_kp."%' GROUP BY No_KP;");
while ($row = mysql_fetch_array($query_viewAduan))
{
if (!$row['COUNT(*)'])
{
echo 'satu';
}
else if ($row['COUNT(*)'] == '1')
{
echo 'dua';
}
else if ($row['COUNT(*)'] == '2')
{
echo 'tiga';
}
}
?>
my problem is that I can't display when the column is empty, but when the column have any value the data it can be displayed
sorry for my poor english...i wanna to update my question...
what i want to do is before i add an value in my database this function will do is count that value what i insert if that value is not inside my database so that this function will add the value inside my database and count it to 1.if that value already at the database this function will count and show it to 2 when the same value insert on second time and also same when the same value insert at the third time will show it to 3
Untested: Assume that you want to know if something has been 'echoed' or not...
This line of code...
$row = mysql_fetch_array($query_viewAduan);
$row will be an 'empty' array if 'no rows returned'. Therefore the 'while' loop will not be entered.
<?php
session_start();
include ("../Connections/connection_db.php");
include ("../Connections/db_connect.php");
$no_kp = '765454104321';
$id = '101';
mysql_select_db($database_connection_db, $connection_db);
$query_viewAduan = mysql_query("SELECT No_KP, COUNT(*) FROM aduan_tidak_hadir WHERE No_KP LIKE '%".$no_kp."%' GROUP BY No_KP;");
$somethingEchoed = false;
while ($row = mysql_fetch_array($query_viewAduan))
{
$somethingEchoed = true;
if (!$row['COUNT(*)'])
{
echo 'satu';
}
else if ($row['COUNT(*)'] == '1')
{
echo 'dua';
}
else if ($row['COUNT(*)'] == '2')
{
echo 'tiga';
}
}
if (!$somethingEchoed) {
// do 'nothing echoed' processing here
}
?>
i've got a problem with my php script. I want to check my script if there is anything in array and if there's no to echo a message but when array is empty it just don't echo nothing.
That's my code:
include("mysql_connect.php");
$name = $_POST['playerName'];
$users = mysql_query('SELECT * FROM playerdata WHERE username LIKE "'.$name.'%"');
if($name==""){
echo 'Type in player name!';
}else{
while($usersList= mysql_fetch_assoc($users)){
if(!array_count_values($usersList)){
echo 'Player not found';
}else{
echo $usersList['username'].', ';
}
}//While end.
}//If name is empty end.
As mentioned, you have to check for rows before the loop.
$rows = mysql_num_rows($users);
if($rows==0) echo "No rows..";
else {
foreach() {
The problem seems is that, if a user enters a invalid name, the while loop does not execute, as the query simply cannot find any rows, the code should be as follows
include("mysql_connect.php");
$name = $_POST['playerName'];
if( $name === "" ){
echo 'Type in player name!';
} else {
$users = mysql_query('SELECT * FROM playerdata WHERE username LIKE "'.$name.'%"');
if ( mysql_num_rows($users) == 0) {
echo "Invalid Player name provided";
} else {
while($usersList= mysql_fetch_assoc($users)){
{
echo $usersList['username'].', ';
} //While end.
}
}//empty name
Notes:
I have re-formatted the code as it looked ugly
The query is executed only if a user provides some text for name, why execute the query when the name is empty !
instead of
if(!array_count_values($usersList)){
use
if(empty($usersList)){
Your while loop wont be executed if $users is empty, so the condition:
if(!array_count_values($usersList)){
echo 'Player not found';
}
will never be met because it is contained in a loop which will never run.
I have output from a select query as below
id price valid
1000368 69.95 1
1000369 69.94 0
1000370 69.95 0
now in php I am trying to pass the id 1000369 in function. the funciton can execute only if the valid =1 for id 1000368. if it's not 1 then it will throw error. so if the id passed is 1000370, it will check if valid =1 for 1000369.
how can i check this? I think it is logically possible to do but I am not able to code it i tried using foreach but at the end it always checks the last record 1000370 and so it throws error.
regards
Use a boolean variable:
<?php
$lastValid=false;
while($row = mysql_fetch_array($result))
{
if ($lastValid) {
myFunction();
}
$lastValid = $row['valid'];
}
?>
(Excuse possible errors, have no access to a console at the moment.)
If I understand correctly you want to check the if the previous id is valid.
$prev['valid'] = 0;
foreach($input as $i){
if($prev['valid']){
// Execute function
}
$prev = $i;
}
<?php
$sql = "SELECT * FROM tablename";
$qry = mysql_query($sql);
while($row = mysql_fetch_array($qry))
{
if ($row['valid'] == 1)
{
// do some actions
}
}
?>
I really really recommend walking through some tutorials. This is basic stuff man.
Here is how to request a specific record:
//This is to inspect a specific record
$id = '1000369'; //**some specified value**
$sql = "SELECT * FROM data_tbl WHERE id = $id";
$data = mysql_fetch_assoc(mysql_query($sql));
$valid = $data['valid'];
if ($valid == 1)
//Do this
else
//Do that
And here is how to loop through all the records and check each.
//This is to loop through all of it.
$sql = "SELECT * FROM data_tbl";
$res = mysql_query($sql);
$previous_row = null;
while ($row = mysql_fetch_assoc($res))
{
some_action($row, $previous_row);
$previous_row = $row; //At the end of the call the current row becomes the previous one. This way you can refer to it in the next iteration through the loop
}
function some_action($data, $previous_data)
{
if (!empty($previous_data) && $condition_is_met)
{
//Use previous data
$valid = $previous_data['valid'];
}
else
{
//Use data
$valid = $data['valid'];
}
if ($valid == 1)
{
//Do the valid thing
}
else
{
//Do the not valid thing
}
//Do whatever
}
Here are some links to some good tutorials:
http://www.phpfreaks.com/tutorials
http://php.net/manual/en/tutorial.php