YaBCK
YaBCK

Reputation: 3029

For loop stopping before get to number needed

My idea is to make tooltip for new users on website. They will go through the tooltips and each time they complete a tooltip it inserts into DB.

However i've got a button which is skip all. So my idea is to insert all the reminder tooltips which they've not completed into the DB with for loop.

So it works if there has been no tooltips clicked already. So the tooltip would be equal to 1 because coming through the $data is equal to 0. However if the tooltip is equal to 1 when passed through $data it gets a +1 and the for loop doesn't seem to post anything into database

$NumberOfTooltips = 2;
(int)$data->tooltipLastID = ((int)$data->tooltipLastID === 0) ? 1 : (int)$data->tooltipLastID + 1;     

for ($x = (int)$data->tooltipLastID; $x <= $NumberOfTooltips; $x++) {
   $query = "";
   $query = "INSERT INTO tooltips ";
   $query .= "(tooltip_id, inserted) VALUES ($x, NOW())";   
   database::query($query);
   $this->id = database::getInsertID();
}

On the broken loop the value of (int)$data->tooltipLastID is 2

Is it because the (int)$data->tooltipLastID is already equal to $NumberOfTooltips?

Upvotes: 1

Views: 86

Answers (2)

Martin
Martin

Reputation: 22760

General improvements

These do not directly solve the question asked but they do give you a helping hand in clarifying your data and steaming out any secondary bugs and bloopers. Also pointing out some best (or at least, better) practise.

  • $x is a lazy and poorly defined counter. Prefer using descriptve veraibles such as $tooltipCounter

  • $data->tooltipLastID should not start at 1; use the same syntax as every other integer number system in PHP/programming and start at zero. If you need a one then add +1 only when it's needed (VALUES (".$x+1.")).

  • $NumberOfTooltips = 2; The number 2 is probably not high enough for adequate testing.

  • var_dump($data->tooltipLastID) and var_dump($NumberOfTooltips) to check both values are what you expect.

  • Rewrite the test code to take the variables out of the code so that you can check your Database connction works correctly (such as if you're trying to insert into a string field-type by mistake)

  • $query = ""; is redundant.

  • You should not need to type cast (int) your object variables ($data->whatever) all the time but type cast them when they're set.

  • Also by adding +1 to a variable PHP automatically recasts the variable as an int anyway.

  • Check that your $data->tooltipLastID is publicly accessible/writable.

  • You use $this ; so which class are you in? Are you self referencing the data class?

  • A bank holiday is just one day.

  • It is better the inserted Database column is set by the database automatically upon insert. You can use this SQL to alter your current table:

       ALTER TABLE <yourTable> MODIFY COLUMN inserted timestamp DEFAULT CURRENT_TIMESTAMP
    

Upvotes: 6

ch271828n
ch271828n

Reputation: 17597

Check the type of $data->tooltipLastID? And plz use var_dump($data->tooltipLastID) and var_dump((int)$data->tooltipLastID) before the for loop to see what indeed the original value and the $x is.

Strange type casts will result in strange bugs...

Upvotes: 3

Related Questions