I found this nifty code http://snipplr.com/view/36868/ the only difference is that my database displays differently, how can I display each of my countries when my database is set up this way. Here is the code:
if( $country = 'Afghanistan' ) $code == 'AF';
I figured that, if not correct me the top should be included this way. Here is the code:
$country = $row['country'];
If that is true I believe although here is where I start getting some troubles It should return the code instead of the country. Here is the code:
return $code;
Does anybody have an idea how I can change my countries into codes when I have the countrys whole name instead of the codes?
try this kode :
function country_code($country){
if($country == 'Afganistan')
$code = 'AF';
//you can add other condition in here like below example
else if ($country == 'Indonesia')
$code = 'ID';
return $code;
}
$code = country_code($row['country']);
echo $code;
Hope this can help you....
EDIT :
Using database
my assumption that you already have a database and table like below..
table name : country
fields :
id
country
country_code
function country_code($country){
$sql = "SELECT country_code FROM country WHERE country='$country' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$code = $row['country_code'];
return $code;
}
$code = country_code($row['country']);
echo $code;
Related
How to add distinct function so that the result is not repetitive?
Any help is much appreciated.
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
while($row= $stmts->fetch(PDO::FETCH_OBJ)){
$pdf->Row(Array($row->busname,$row->business));
}
}
Here is the output of my code:
I'm using fpdf to display my data from database. And its working fine. Its just that the name is repeating.
This is what I want to achieve:
or what if i want to display this?
option 2
To achieve your desired result, you need to check if the current business name is the same as the last displayed one, and if so, not output it:
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
$busname = '';
while ($row = $stmts->fetch(PDO::FETCH_OBJ)) {
$thisbusname = ($row->busname != $busname) ? $row->busname : '';
$pdf->Row(array($thisbusname,$row->business));
$busname = $row->busname;
}
I am trying to pull dynamic content from a database based on the wildcard subdomain.
Please see the code below.
<?php
$data = $_GET['get'];
$data = addslashes(str_replace('-',' ',$data));
$town_result = $database->getData("select * from nationwide where town = '$data' ");
if( is_object( $town_result ) )
{
$area = $town_result->fetch_assoc();
}
else
{
$region_result = $database->getData("select * from nationwide where region = '$data' ");
if( !is_object( $region_result ) ) {
header('Location: http://example.com');
}
$area = $region_result->fetch_assoc();
$area['town'] = $area['region'];
}
?>
I have tried changing = '$data' "); to LIKE '%" . $data . "%'"); but this only pulls the first row in my database.
You can review how to use fetch_assoc to get associative records.
https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
You can print $area, then you can verify either array count is 1 or more than 1.
Or best way, you can print sql query and run directly in your db.
First of all, im a beginner so please excuse me if i don't mention things that could be important.
I have Sellers, that are in different countries in the world. Each country belong to a continent. Im trying to:
get the seller name
Fetch Database to see in what countries (China,Italy,Germany...) he offers his goods.
Then fetch another database and see in what continents (ASIA,EU) belong each country
Store the countries (Italy,Germany) to a variable $country_eu and $country_asia (China). So later i will add this variables to update database fields.
if($continent == 'ASIA') is not included in the code YET since i take one by one the steps. I want to make first EU work, then i figure out the rest.
include_once('../db.php');
session_start();
$seller_name = $_SESSION['seller_name'];
echo $seller_name;
if (!empty($_POST['flag_image'])){
$flags_array = $_POST['flag_image'];
$flags_string = implode(",",$flags_array);
}else{
$flags_string = '';
}
$cont_eu = '';
// Assign Flags to Continents
foreach ($flags_array as $flag_image){
$qry_find_flag_continent = " SELECT continent FROM flags WHERE flag_image = '$flag_image' ";
$result = $dbdetails->query($qry_find_flag_continent);
while($row = mysqli_fetch_array($result)) {
$continent = $row["continent"];
if ($continent == 'EU') {
$cont_eu .= $flag_image;
}
}
echo $cont_eu;
The problem with my code is that when i echo $cont_eu is giving me just 1 result(Germany) and not the EXPECTED (Italy,Germany)
Thank you all for your precious time and attempts to help. Here is the new code that works great.
include_once('../db.php');
session_start();
$seller_name = $_SESSION['seller_name'];
echo $seller_name.'<br>';
if (!empty($_POST['flag_image'])){
$flags_array = $_POST['flag_image'];
$flags_string = implode(",",$flags_array);
} else {$flags_string = '';}
// Assign Flags to Continents
foreach ($flags_array as $flag_image){
$qry_find_flag_continent = " SELECT continent FROM flags WHERE flag_image = '$flag_image' ";
$result = $dbdetails->query($qry_find_flag_continent);
while($row = mysqli_fetch_array($result)) {
$continent = $row["continent"];
if($continent == 'EU'){ $eu_flags_array[] = $flag_image;}
}
}
print_r($eu_flags_array); //Gives me Array ( [0] => Germany.png [1] => Italy.png )
I have a webpage with a button on it. When the button it clicked it sends a request to a page with this code on it
$userName = "tdscott";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$divID = explode('?', $url);
$id = 0;
$id = explode('#',$divID[1])[1];
$func = $divID[2];
$find = mysqli_fetch_array(mysqli_query($con,"SELECT likes FROM status WHERE id='$id'"))['likes'];
if ($func == "addLike")
{
$promoted = $userName . "-";
mysqli_query($con, "UPDATE status SET promotedBy = CONCAT(promotedBy,'$promoted') WHERE id='$id'");
$find++;
mysqli_query($con,"UPDATE status SET likes = '$find' WHERE id='$id'");
echo $find;
}//end if addLike
elseif($func === "removeLike")
{
echo "ERROR";
}//end if removeLike
elseif ($func === "getLikes")
{
echo $find;
}//end if getLikes
mysqli_close($con);
I left of the database connection information. But for some reason when this is called it produces inaccurate results. For example... Sometimes it will put multiple instances of $promoted in the promotedBy field in my table and sometimes it will update other rows in the table that the id does not equal the current $id. I am wondering if somehow it is getting the $id variable mixed up from when I submitted it with a different value before. Is there a way to reset the variables before I call it each time?
Please note: In the if statement, we are only looking at the addLike portion. I included the other just in case it was causing the problem.
unset($id);
Sorry should have done more research.
I have some search functionality that works with 3 drop down boxes. Based on the criteria chosen, a profile is returned. The 3 drop downs are:
County
Constituency
Gender
Now I am trying to build a query but have just realised that actually a person does not have to choose an option from each drop down and nor do I want them to.
So for instance I do not want to disable the search button until an option is selected from each drop down.
Having chosen a value from any drop down, and possibly having no value selected from any drop down at all, and just clicking the search button, I am trying to understand how I can cope with the unknown combinations.
My first thought was that I could use something like a truth table but I imagine this is simply overkill and in fact this is a very common piece of functionality.
Then I thought maybe I could have something like:
$county = "$_GET['county'];";
$constituency = "$_GET['constituency'];";
$gender = "$_GET['gender'];";
Then I could check to see if they are empty and somehow use this value, e.g.
if($county !== '') {
???SOMEHOW MAKE USE OF THIS IN AN SQL QUERY???
PERHAPS PASS IT TO ANOTHER PARAMETER
$sqlparams = "county = '$county'";
}
SELECT * FROM profile
WHERE {$sqlparams};
I think I'm on the right tracks but could use some guidance.
All help is greatly appreciated.
This should do want you want, I think.
<?php
$tooLookFor = array("county", "constituency", "gender");
foreach($tooLookFor as $key){
if(isset($_GET[$key]) && strlen($_GET[$key])>0){
$queryParams[] = $key.'="'.$_GET[$key].'"';
}
}
$query = "SELECT * FROM profile WHERE ".implode(' AND ', $queryParams);
?>
You could do something like:
$county = $_GET['county'];
$constituency = $_GET['constituency'];
$gender = $_GET['gender'];
$sqlparams = array();
if($county !== '') {
$sqlparams[] = "county = '$county'";
}
if($constituency !== '') {
$sqlparams[] = "constituency = '$constituency'";
}
if($gender !== '') {
$sqlparams[] = "gender = '$gender'";
}
$query = "SELECT * FROM profile";
if (count($sqlparams) > 0) {
$query .= " WHERE " . implode(" AND ", $sqlparams);
}
You can do that with something like this:
$where = array();
//repeat as needed
$where[$column] = $value;
$where2 = array();
foreach($where as $key => $value){
$where2[] = "$key = '$value'";
}
$where_string = implode(' AND ', $where2);
$where_string will have the string to insert after WHERE.
Yes, you are on the right track, you're just not at the right switch yet. ;)
You can't build the query until you know what you have to work with. So first, in your validation, determine (as you are doing) with the key words actually are and what fields they represent. Presumably these map to fields in tables, maybe 3 tables? Point is, your query will need to be dynamically built.