Use php to upload to web service rather than ajax

usort() uses the function you supply to it (the section within the {} ) to decide how to sort the array. The function returns either -1 for a>b, +1 for a<b, or 0 for a==b. To reverse the sort order, either swap the -1 and 1 values around, or swap the < and > positions, so that the comparison works the opposite way around. You might even just be able to reverse $a and $b in the function definition on the first line. If you want to sort on a different field (let’s say you had distance from airport, or number of good reviews, or whatever) just change the field name you use for the comparison.

Oh this is sweet droopsnoot, its absolutely perfect to get this working as it was.

So if for instance I only want to show 5 star hotels, and then key is $nt[‘IdCat_Hot’], so could I use the same format so that $nt[‘IdCat_Hot’] is equal to 5.

The oldest to newest and newest to oldest is sorted as you explained.

I’m not sure what you mean by that - if you want to include only five-star hotels, then that would be done as part of your query rather than part of the sort routine. If the web service can’t have that specification, then just exclude any that don’t meet the requirement before you merge them into the main array.

Morning droopsnoot, I’m getting there slowly, but have hit a mental snag.

Passing the array around using the session cookie is great, but am now on the section that displays the hotels depending on what radio button they choose and then what they type, so for instance they can choose ‘Country’’ and then type ‘Spain’, all not to bad though but what I need to do is count the number of values in the array in a certain field with a certain value.

For instance if they type ‘Spain’, in the db it has a value of ‘1’, I have that sorted using the code below, but I need to count those values. I have this below.

$mainArray = $_SESSION['mainarray'];

if ($radioB == "country") {
$query="select IdCntry_Hot from tbl_hotels LEFT JOIN tbl_countries ON (tbl_countries.Id_Cntry=tbl_hotels.IdCntry_Hot) where tbl_countries.Nom_Cntry like '%$in%' AND TourCheckActive=1";
$resulta = $dbo->prepare($query); 
$resulta->execute(); 
foreach ($dbo->query($query) as $nt) {
$country_Hotel = $nt['IdCntry_Hot'];
}
$num_rows = $mainArray;
$counts = array_count_values($num_rows);
$num_rows = count($counts['IdCntry_Hot']=='1');
echo $num_rows;

}

So above $country_Hotel using ‘Spain’ would have the value of 1, but Im not quite sure as you can see, how I can scan through the array and look in all the IdCntry_Hot fields and count how many have the value of 1

Surely if they choose to specify a country, and that country is Spain, when you do the query, all the results will be in Spain? So you just need to count how many rows you get back from the database? Or maybe I am missing something in the database layout.

If not, I don’t think an array is needed:

$inSpain = 0;
foreach ($dbo->query($query) as $nt) {
if ($nt['IdCntry_Hot'] == '1') $inSpain++;
}
echo $inSpain;

It doesnt work like that, what happens is they choose a radio button and its for example ‘Country’ then they type say Spain, but in the db Spain if they type it correctly is equal to 1, but i need to do the query to find out what the value of the country is, then with the array as its already been created, I need to see how many hotels are in the already made array that equate to for example ‘1’.

If all you need is the number of hotels in a country, you might be able to skip the PHP array stuff and get the count directly as a single result with a query something like

SELECT COUNT(IdCntry_Hot) AS hotel_count 
FROM tbl_hotels 
LEFT JOIN tbl_countries 
ON tbl_countries.Id_Cntry = tbl_hotels.IdCntry_Hot 
WHERE tbl_countries.Nom_Cntry LIKE '%$in%' 
AND TourCheckActive = 1

Ah, OK, I wondered if that might have been a bit too obvious. Looking at it, I don’t think you can use array_count_values() with your multi-dimensional array, I suspect you might have to do it longhand:

$count = 0;
foreach($mainArray as $hotel) {
  if ($hotel['IdCntry_Hot'] == $country_Hotel) $count++;
  }

Maybe there’s a quicker way, I don’t know what it is.

ETA - I think one thing confusing me is why you’re querying the hotels table to translate the country name into the country code, is that strictly necessary?

1 Like

Ye there 2 db’s though, and the array is the almagamation of the two db’s ones a web service the other local, and I have to query the local db to get the value of the hotel they have typed. Then I have to go back to the array with that numerical value to count the number of hotels that have that value

No worries droopsnoot, I’ll change to this and work from there instead :+1:,

And ye I have to query it, as when the hotel is put into the db via the admin its given an auto increment value, and when they type the word spain it gives a value of say 1, then wen the hotel goes into the db, one of the fields that hold all the info for the particular hotel has that numerical value.

Excellent, cheers droopsnoot, that worked perfectly again. Bit less chunky than my version, but you got it

Yes, but what I mean was can’t you get the relationship between the country name and the country id just from tbl_countries, without involving tbl_hotels? It seems that the name is in tbl_countries.Nom_Cntry, and the number is in tbl_countries.Id_Cntry, so I thought you could do

$query="select Id_Cntry from tbl_countries where Nom_Cntry like '%$in%'";

Having said that, I don’t know what “TourCheckActive” does, so maybe that stops it.

Ah ye your right, that as you know was doing something else before I started swapping things around, and i trimmed the statement down but didnt then notice that I could trim it even further.

The code below isntnt working like the others, but it is different I agree as basically with this one, rather than you having a word such as ‘Spain’ then checking to give it its 1 value, this is a search by name of a hotel, so they may only in some cases type a part of the hotel name as they not sure what the rest is, so its not like country type equal to country in the db and then give me the number, this is look for a hotel that has a name like this, then count the number of them.

if ($radioB == "hotel") {
$names_Hotel = null;
$query="select Nom_Hot from tbl_hotels where Nom_Hot like '%$in%'";	
$resulta = $dbo->prepare($query); 
$resulta->execute(); 
foreach ($dbo->query($query) as $nt) {
$names_Hotel = $nt['Nom_Hot'];
}
$count = 0;
if (is_array($mainArray)){
foreach($mainArray as $hotel) {
if ($hotel['Nom_Hot'] == $names_Hotel) $count++;
}}
$num_rows = $count;
}

Then I’m trying to put put the hotels with below, but its only showing 2 when I know there more than that, its a bit strange

if(!empty($names_Hotel)) {
$i = 1;
if (is_array($mainArray)){
foreach ($mainArray as $key => $hotel) {
if (strpos($hotel['Nom_Hot'], $names_Hotel)!==false) {

}    
$i++;
if($i>$showLimitOne) break;

The curly brackets might not be the right number above as have chopped out all the delivery html code in between, but that works fine anyway

I’m confused by the first section of the code, where you search for a single hotel in tbl_hotels that matches the search string that the user typed in. If you’ve already got the array built, wouldn’t it be better to just do the second bit of code against the array and see if any of the names match?

1 Like

So sort of bypass the first section and go straight to the search and show from the array, something like below.

if ($radioB == "hotel") {
$i = 1;
if (is_array($mainArray)){
foreach ($mainArray as $key => $hotel) {
if (strpos($hotel['Nom_Hot'], '%$in%')!==false) {

}    
$i++;
if($i>$showLimitOne) break;

That’s it, though your strpos syntax needs to lose the SQL-specific bits. Just use $in.

lol fair play droopsnoot, did that and the 7 showed, but still need to try and get the count for the pagination, as the first chunk was doing

Also I think I will have to capitalise everything that has a name in the array, or capitalise it before it goes into the array. It seems that sometimes there a number of hotels that for some reason start with a small s and others with a capital, and it is having an impact in the search results.

Or

if (strpos(strtoupper($hotel['Nom_Hot']), strtoupper($in))) { 

upcase them just for the search.

Search will be interesting to imagine what users mean vs. what they want, especially any hotel with punctuation in it for example. Perhaps write a function to strip anything other than alpha characters out and use that instead of strtoupper above.