Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Little help on While Loops with an Array.

Featured Replies

Hi guys, hope you could help with the following code:

 

/** Check users Tickets.
*/
 public function checkUserTicket() {
 $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'";
 if ($sth = $this->db->query($sql)) {
  $count = $sth->fetchColumn();
  if ($count >= 1) {

   while ($i1 = mysql_fetch_array($sth)){
 echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';
   }

  } else {
   echo '<h5 class="subheader centered">You currently do not have any existing tickets.</h5>
    <h1 class="centered"><a class="uibutton loading" title="Creating New Ticket" rel="1" onclick="location.href=newticket.php;">New Ticket</a></h1>';
  }
 }
}

 

The top half is working correctly ($count) it picks up I have a support ticket under my email address and moves onto the while loop, which should display the all tickets I have.

 

Issue I'm having is:

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given

 

No idea what that means :/

 

URL

http://linkthemup.com/ltusupport/

 

Login is:

admin

password

 

Thank you! Much appreciated.

looks a little complicated to me. If it were me I'd just use a variable to say whether anything has been found by the SQL statement and once the loop is over if it's still 0 then echo your not found message.

do a var_dump on $sth and see what it outputs. You seem to be using an object and a standard function for databases simultaneously which shouldn't be done. I would imagine the values in $sth are an array of results, and that you just need to use a foreach for those

  • Author

I have added the Var Dump. I have no idea what that means..

object(PDOStatement)#4 (1) { ["queryString"]=> string(64) "SELECT * FROM tickets WHERE member_email = 'dean@linkthemup.com'" }

 

What would be my object? the $this? or the $sth?

My standard function being mysql_fetch_array?

 

Soory for being a noob, but like I said only PHP I know is what I got from Google haha!

 

Thank you for trying to help me..

You cant use mysql library functions on PDOStatement objects. Its like trying to play a SNES game in an PS3.

 

You need to use PDO to fetch the result

 

while($i1 = $sth->fetch(PDO::FETCH_OBJ)) {
echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';
}

Edited by Jock

  • Author

Hmm, OK..first time I've ever heard of PDO.

 

Thanks Jock, I put that in:

 

if ($count >= 1) {

 

while($i1 = $sth->fetch(PDO::FETCH_OBJ)) {echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';}

echo 'test';

}

 

It has fixed the error ,but does not display anything else I'm afraid. Added the test echo to make sure the if was being called.

Was it being called?

 

You should dump fetchAll and columnCount to see if the query is working correctly.

  • Author

Thank you Jock, I put two var_dumps in:

 

 public function checkUserTicket() {
 $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'";
 if ($sth = $this->db->query($sql)) {
  $count = $sth->fetchColumn();
  if ($count >= 1) {
  echo 'Var-Dump Count:<br>';
  var_dump($count);

  while($i1 = $sth->fetch(PDO::FETCH_OBJ)) {echo 'Subject: $i1->ticket_subject - $i1->ticket_date - Status: $i1->ticket_responded - View Ticket.<br>';}
   echo '<br><br>Var-dump $sth->fetch(PDO::FETCH_OBJ):<br>';
   var_dump($sth->fetch(PDO::FETCH_OBJ));
   echo '<br><br>Picked up a Ticket, should var_dump $count and $sth->fetch.';
  }

 

Result is:

 

Var-Dump Count:

string(1) "1"

 

Var-dump $sth->fetch(PDO::FETCH_OBJ):

bool(false)

 

So the $count does pick up the the one ticket.

Its just the while loop :/

Ok so do this to see what is being pulled out.

 


public function checkUserTicket() {
   $sql = "SELECT * FROM tickets WHERE member_email = '$this->email'";
   if ($sth = $this->db->query($sql)) {
       print_r($sth->fetchAll());
   }
}

  • Author

Hi Jock, thank you for your'e reply.

Sorry to drag this out, but the code you offered has created an error:

 

/** Check users Tickets.
*/
public function checkUserTicket() {
$sql = "SELECT * FROM tickets WHERE member_email = '$this->email'";
if ($sth = $this->db->query($sql)){
print_r($sth->fetchAll());
}
}

 

Parse error: syntax error, unexpected T_STRING in /home/ltusupport/public_html/ltusupport/lib/core.php on line 109

Line 109 is the print_r

 

I'm going to Google it, attempt to fix it myself as I don't wish to just leach from you.

Thanks for your help so far.

Edited by Bravo81

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.