Can I use select COUNT(*) in pdo

Ah, my mistake. I was referring this piece of code:

$stmt->execute(["%$_POST[id2]%", $_POST['id']]);
                          ^ here

I thought it would create an error because, while there are quotes around the entire first string, there are no quotes around the array index id2 within that string. Turns out that they are not required there, because it’s within a double-quoted string. I used to have a signature to show I’m learning PHP, but I don’t always remember to write it.

My guess would be that it is still an error, but that execute somehow “absorbs” the “undefined CONSTANT found, assuming string. string used” bit.

Fine I guess, until when / if PHP changes that.

IMHO safest to use quotes there and not worry about possible breakage in the future.

Ok back in work now as have been keen to get these updates sorted and see, and although dreamweaver now accepts those line changes the loop isnt working correctly, but I have managed to get it working on the live server so I can show you guys.

Project Area

If you type Cancun into the text area the first 9 hotels will appear, and the second ‘Show More’ button, which all works fine.
But on pressing the ‘Show More’ button again, it correctly shows the remaining hotels, but it loops again and shows the same last batch of hotels again.

Am going to keep on working in my dev area to try and resolve it, but will leave the live one as it is.

What’s the code like at the moment? I can see that there are issues with how it populates on the second and subsequent runs, as you said.

ETA - That’s strange. It seems to vary randomly. Once I typed in Cancun and it displayed as you said - the first group correctly, the second group repeated. Another two times, I typed in Cancun and it displays the first group, then the second group twice and prompts for a third group, but seems to include places that don’t match the search word.

Yep its a strange one, and so far this morning ive been trying to counter the loop with count++ but thats not working, it hasnt changed much but the php related to this second display is -

<?php
if(!empty($_POST["id"]) && !empty($_POST["id2"])) {

$queryAll="select count(*) from tbl_hotels LEFT JOIN tbl_resorts ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE tbl_resorts.Nom_Rsrt like ? AND Act_Hot='1' AND (tbl_hotels.Id_Hot < ?) ORDER BY tbl_hotels.Id_Hot DESC"; 
$stmt=$dbo->prepare($queryAll);
$stmt->execute(array("%$_POST[id2]%", $_POST['id']));
$count = $stmt->fetchColumn();
$showLimit = 9;

$query="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot, Foto1_Hot from tbl_hotels LEFT JOIN tbl_resorts ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE tbl_resorts.Nom_Rsrt like ? AND Act_Hot=1 AND tbl_hotels.Id_Hot < ? ORDER BY tbl_hotels.Id_Hot DESC LIMIT $showLimit";
$resultb = $dbo->prepare($query); 
$resultb->execute(array("%$_POST[id2]%", $_POST['id'])); 
$num_rowsb = $resultb->rowCount();

if($num_rowsb > 0){
foreach ($resultb as $nt) {
$tutorial_idb = $nt['Id_Hot']; ?>
<div class='obj'><a href='#'><p class='text'>REQUEST REPORT 2 <?php echo $tutorial_idb; ?></p><div class='item'><img src='http://www.checksafetyfirst.com<?php echo $nt['Foto1_Hot']?>' alt='' width='360' height='239'><div class='item-overlay top'></div></div></a><p class='nameHotel'><?php echo $nt['Nom_Hot'] ?></p><p class='addressHotel'><?php echo $nt['Dir_Hot'] ?></p></div>
<?php } ?>
<div class="obj push"></div>
<div class="obj push"></div>
<div class="pushend"></div>
<?php if($count > $showLimit){ ?>
<div class="show_more_main" id="show_more_main<?php echo $tutorial_idb; ?>">
<span data-id="<?php echo $tutorial_idb; ?>" data-id2="<?php echo $_POST['id2']; ?>" class="show_more" title="Load more posts">SHOW MORE SECOND <?php echo $tutorial_idb; ?></span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading….</span></span><br/><br/>
</div>
<?php } } }?>

The AJAX hasnt changed, as I dont believe its that. But to test it was working I put the hotel id into the hotel div and it shows up on rollover and then the last id shows up next to the show more button, so its working fine, just for some reason it loops around again at the end

OK, the reason it sometimes displays things that don’t match my search input is down to how it goes off to search based on the search box changing, rather than waiting for a “submit” button - I just had a look at my test search, and it’s using “can” rather than the full word. So that probably isn’t relevant.

Out of interest, what’s the reason for having this line twice?

<div class="obj push"></div>
<div class="obj push"></div>

They are to keep the last line of hotels to the left, if they arent there the last 1 or 2 line up centrally.

.obj
{
width: 360px;
height: auto;
background-color: white;
display: inline-block;
margin: 10px;
text-align:center;
border:#666 solid 1px;
box-shadow: 3px 3px 3px #888888;
}
.obj.push {
height: 0px; border:#666 solid 0px;
}
.pushend {
width: 100%;
height: 0px;
display: inline-block;
}

Ah, OK, Css-y things.

One thing puzzles me, that is the div called .tutorial_list doesn’t seem to just have the list of new hotels appended, it has another div with the same name appended within it. It’s a long time since I’ve looked at CSS in any detail, I wondered if that mattered. It seems that your second-search code is sending it as part of the response. At the start you have

Then after the first timey you press “next”, you have

And then

I would have expected it to just append extra “div class=obj” hotel details.

This is what comes back from the request for extra hotels, obviously with the right-hand side cut off.

Apart from anything else, it keeps opening a new DIV that it isn’t closing. I’m not sure how much difference that makes.

Oh right, umm the displayDiv div is the area given to the page to allow the hotels to appear when you search by type, and then tutrial_list is whats allowing the hotels to appear inside of in an orderly way.

I shall have to keep looking at this to see if this is the problem maybe, as i havent quite worked it out yet, but wanted to reply to you straight away

It just seems that the second-and-subsequent search code shouldn’t be re-opening it every time, just sending the content. Might not make any difference, of course.

Ye I understand what your saying, even if it doesnt fix it it should just append as you say to the original one.

<script>
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var id = this.dataset.id;
var id2 = this.dataset.id2;
$('.show_more').hide();
$('.loding').show();
$.post('ajax-search-demock_3.php', this.dataset, function (html) {
$('#show_more_main'+id).remove();
$('.tutorial_list').append(html);
});
});
});
</script>

This is the AJAX that clicking the show more button always goes to

That does append it - the issue is that the response from the PHP code includes a div opener as well as all the hotel details, so that gets appended and opens another div.

Like I said, though, it might not make any difference to the problem itself.

Well droopsnoot, you may have hit one nail on the head

I changed this line - $(‘.tutorial_list’).append(html);

to $(‘#displayDiv’).append(html);

And it stopped the extra loop but it creates new div’s.

Have updated the live site

Yep I think thats it mate, thank you very, very much. Seems like a bit of CSS tinkering now and I’m sorted, and relax, yes blood pressure reducing and heart rate returning to normal :pray:

Oh, that’s good. It would be interesting to know exactly why that made a difference, but not as interesting as it doing what it needs to.

Ye I bet it’ll turn out to be a fudge now, but never the less it works, and its error free, so will keep it.

Think we both learned a lot there

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.