I have a MySQL table with reports. Fetching my reports is used by function:
function fillItByRoute($lat){
global $db;
$array = array(array());
$result = $db->query("SELECT * FROM reports WHERE latitude LIKE '$lat%' ORDER BY datetime_view");
while ($row = $db->fetch_array($result)){
$array[$int][1] = "Desc";
$array[$int][2] = $row['latitude'];
}
return $array;
}
which creates an multidimensional array.
[latitudes in MySQL are stored like 25.33236645, 23.2665666 etc...]
I have a different array:
$array_lat = array(25.5,23.1,45.2);
So, I would like to know if it is possible to get through array values of $array_lat with checking the match with values from multidimensional array created by function fillItByRoute() and storing values of function fillItByRoute() in new array?
One more time, shortly:
There are different values in $array_lat;
I want to check function fillItByRoute() with input of $array_lat
Results, which suit have to be stored in new array.
The new array should be with values (using values of my $array_lat): 25.5666332, 25.511433, 23.1233, 23.11444, 23.1, 45.269...etc, could be even hundreds of items.
Is it possible to do something like that?
Thank you very much!
I think you want something like this. It iterates through your array_lat and compares it to a value from SQL. I'm not entirely sure what you want to do with the comparison but you should put it in the if statement from the second function. If you want a new array it looks like you understand how to create those but let me know if this is too ambiguous.
function fillItByRoute($lat){
global $db;
$array = array(array());
$result = $db->query("SELECT * FROM reports WHERE latitude LIKE '$lat%' ORDER BY datetime_view");
while ($row = $db->fetch_array($result)){
$array[$int][1] = "Desc";
$array[$int][2] = $row['latitude'];
}
return $array;
function compareArrToSql($array_lat){
for($curr_lat = 0; $curr_lat<count($array_lat); $curr_lat++){
$sql_val = fillItByRoute($array_lat[$curr_lat]);
if($array_lat[$curr_lat]==$sql_val){
}
}
}
If my assumption about what you meant is correct, this is what you are after:
function fillItByRoute($lat){
global $db;
$array = array(array());
$result = $db->query("SELECT * FROM reports WHERE latitude LIKE '$lat%' ORDER BY datetime_view");
while ($row = $db->fetch_array($result)){
$latitude = number_format((float)$row['latitude'], 2, '.', '');
if (in_array($latitude,$lat)) {
$array[$int][1] = "Desc";
$array[$int][2] = $row['latitude'];
}
}
return $array;
}
Related
I need to be able to check if certain values are present in title_id, but as title_id's are index's it won't allow me to do this. Is there a way to store the values of the index without storing the array itself?
I have been searching for this answer for a long time but anything I search pulls up answers to different questions.
$selects = (array) null;
$res = mysqli_query($datab, "SELECT title_id FROM selects") or die($datab->error);
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab;
}
///////////// part of a later query
if (!in_array($thisid, $selects)) {
$title[] = $datab;
$j++;
}
$selects is array of arrays, not array of title_id.
Try something like that:
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab['title_id'];
}
I often need to retrieve results and access them by a given column.
Is there a way to write this without walking through the whole dataset each time?
I looked into various PDO fetch modes, but nothing jumped out as being simpler than that. Thanks.
function get_groups() {
$stmt = $dbc->prepare("SELECT * FROM groups ORDER BY group_name");
$stmt->execute();
$groups = $stmt->fetchAll();
$return = [];
foreach($groups as $group) {
$return[$group['id']] = $group;
}
return $return;
}
My proposed solution was pretty obsolete. The right solution comes from this answer
$stmt = $pdo->query("SELECT foo,baz FROM bar")
$groupedByFooValuesArray = $stmt->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_UNIQUE)
to group it by another column, change the first column you select
if your goal is to have your same array indexed by different column values, I think the only option is to actually index them by different column values.
you can do that by controlling by which field the array is returned
function get_groups($sql,$key,$values='') {
if ($values != '') {
$stmt = $dbc->prepare($sql);
$stmt->execute($values);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
else {
$rows = $dbc->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
foreach ($rows as $row) {
$indexed[$row[$key]] = $row;
}
return $indexed;
}
then, use get_groups to build an indexed array:
$sql = 'SELECT * FROM groups ORDER BY group_name'
var_dump(get_groups($sql,'id'));
There might be a way to store a resultset in some way that you can fetchAll() it multiple times, that would be awesome. but I don't know it.
Working on a e-shop, i must read from the DB the products that have productpack not null.
productpack from the DB looks like this : 0141,3122,0104,0111,3114,0106,0117 .
I'm trying to get all the DB items that have productpack set (not null), and make them into an array with arrays with those codes (0141,3122,0104,0111,3114,0106,0117).
function p_productpacks(){
$productpacks = array();
$pack = array();
$q = mysql_query('SELECT productpack FROM products WHERE productpack <> "";');
while($p = mysql_fetch_object($q)){
$pack = explode(",", $p);
$productpacks[] = $pack;
}
return $productpacks;
}
You need to create an array otherwise, you're overwriting existing packs:
$pack = explode(",", $p->productpack);
$productpacks[] = $pack;
For more information read about array_pushDocs and PHP Arrays Docs (and mysql_fetch_objectDocs).
You can get all productpacks in a CSV array using:
$result = mysql_query("SELECT GROUP_CONCAT(productpack) as productpacks
FROM products WHERE productpack <> '' ");
if ($result) {
$row = mysql_fetch_row($result);
$productpacks_as_CSV_string = $row['productpacks'];
}
That way you only need to get one row out of the database, saving lots of time.
See: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
mysql_fetch_object returns an object and can't be used with string processing.. if you definitely want to use this, you need to do something like:
$pack=explode(',',$p->productpack);
$productpacks[]=$pack
Or, to use a good old array instead:
while ($p = mysql_query($q))
{
$pack = explode(",", $p['productpack']);
$productpacks[] = $pack;
}
foreach($scraperSites as $site) {
//$scraperWriter->addSite( new ScraperSite($site) );
print_r($site);
}
scraperSites is the array of all sites from the mySQL database; I'm trying to keep $site as an array, (but only with one row worth of data), add it to an object, then move on to the next row.
After looking at your code, I think this is your problem:
public static function getScrapedSites($db) {
$query = "select * from sites";
$result = $db->query($query);
$scrapedSites = $result->fetch_assoc();
return $scrapedSites;
}
I believe this will always return a single row. You need to loop over the results with fetch_assoc() and append the results to an array.
public static function getScrapedSites($db) {
$query = "select * from sites";
$result = $db->query($query);
$scrapedSites = array();
foreach($result->fetch_assoc() as $site) {
$scrapedSites[] = $site;
}
return $scrapedSites;
}
You said that $site was outputting a string in your example. This is because your foreach loop was iterating over the db fields in the associative array.
function showFields($selClient)
{
$result = mysql_query("SELECT * FROM project WHERE projectid = $selClient");
$values = array();
while($row = mysql_fetch_array($result))
{
$values[] = array($row['clientname'],$row['prospect'],$row['salesperson']);
}
return $values;
}
When i return the values to Flex, i am not able catch individual elements. When i trace i get all values stored in a single array...
I am slightly confused...,.
var editField:Array = event.result as Array;
Alert.show(editField[0]);
This returns all the values in the Array, instead of the 0th element.
you could do:
Alert.show(editField[0][0]);
if i understand it correctly...
you need to iterate over two arrays (two levels)
Also, if you are only returning specific columns, why select them all? This will function will return the same data and as it only selects what you need, will be faster. In this case the select is so simple that the time difference will be virtually 0, but it's a good habit to get into for when your db queries start getting more complex.
function showFields($selClient)
{
$result = mysql_query("SELECT clientname, prospect, salesperson FROM project WHERE projectid = $selClient");
$values = array();
while($row = mysql_fetch_array($result))
{
$values[] = $row;
}
return $values;
}