Can you add a count digit to the end of a $row->variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you add a count digit to the end of a $row->variable?

Post by simonmlewis »

I have a database with 12 fields (among others), and want to be able to produce what is in them, on the fly.

So rather than saying 12 times over:

Code: Select all

if ($row->titletick1 != NULL) { ... }
if ($row->titletick2 != NULL) { ... }
and so on....
I want the $row->titletick1 to be entered via a count. So if my client wants add a further 5, I just enter them into the database, and let the web site code handle the rest.

I'm pretty sure it's something like this, but not sure:

Code: Select all

$tickcount = 1;
while ($tickcount <= 12)
{
$titletick = $row->titletick . $tickcount;
$tickcount = $tickcount + 1;
echo "$titletick <br/>";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can you add a count digit to the end of a $row->variable

Post by s.dot »

You can make variable variables... $row['titletick' . $count]

Code: Select all

$it = 0;
while (whatever)
{
    $row['titletick' . $it] = $whatever;
    $it++;
}
However, this is best suited for an array.

Code: Select all

while (whatever)
{
    $row['titletick'][] = $whatever;
    //print_r($row);
}
And your database structure would insert a row into a database table rather than having a limited number of named columns.
Instead of titletick1, titletick2, titletick3 in a table You would have your own table with each row for a titletick.

Structure id | user_id | titletick

Then you could just SELECT * FROM `tablename` WHERE `user_id` = '$member'

This will allow for any number of titleticks.

Sorry if I misunderstood your question.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

Sorry I don't under that the "= whatever" part. What is the "whatever" meant to mean?

I have to use that $row->***** to query the database.
So it will count thru 12 of them, and if it find a result for $row->titletick1, 2 and 3, it will do things. If it doesn't find one for 4, it won't, and so on.

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can you add a count digit to the end of a $row->variable

Post by s.dot »

In that case, if your structure is correct, I think you want this.

Code: Select all

$tickcount = 1;
while ($tickcount <= 12)
{
    //variable variable for titletick count
    $titletick = $row->{'titletick' . $tickcount};

    //see if it exists
    if (!empty($titletick))
    {
        echo $titletick . '<br />';
    } else
    {
        echo 'title tick ' . $tickcount . ' does not exist';
    }

    $tickcount = $tickcount + 1;

}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

Not quite - I think you are concentrating more on showing if something does not exist - rather than showing what if it DOES exist.
If it doesn't, I just do nothing with it.

But if $row->titletick1, and $row->titletick2 etc DO have content, then I want to produce the results of that:

via echo "$row->titletick1".... but obviouslly cannot put it like that because it needs to be merged in the other "variable variable" you've coded here.

What I suppose I meant was, can you say:

echo "$row->titletick$count". Or echo "$row->titletick . $count" ?

That kind of thing.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can you add a count digit to the end of a $row->variable

Post by s.dot »

Yes.

echo $row->{'titletick' . $count} would produce for example, echo $row->titletick5;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

So I could therefore do a query:

Code: Select all

$count = 1;
while ($count <= 12)
{
if ($row->{'titletick' . $count} != NULL)
{
echo "$row->{'titletick'.$count} is available, look at stock code $row->{'stockcodetick' . $count}";
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can you add a count digit to the end of a $row->variable

Post by s.dot »

Yes. It is still more suitable for arrays, though. There's an old rule I heard.. "If it ends in a number, it probably belongs in an array.".
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

So how would do you the above that I have done, in an array, as I don't think your first array was the answer. With respect to a fellow coder!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can you add a count digit to the end of a $row->variable

Post by s.dot »

Thank you. I have no idea as I don't know your database structure for this or even what a title tick is or the purpose of the code.

However I think title ticks should be it's own table with the structure ID | USER_ID | titletick
Then you would query for all of the rows. Then use mysql_fetch_assoc() to get the rows, and loop through it.

But if it works, it works. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

Why is this timing out?

Code: Select all

$romancount = 1;
  while ($romancount <= 12)
    {
    if ($row->{'romancodetick' . $romancount} != NULL)
      {
      $romancodetick = $row->{'romancodetick' . $romancount};
      $titletick = $row->{'titletick' . $romancount};
      
      $resultrccheck = mysql_query ("SELECT rcstock FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'");
      $num_rcstock = mysql_num_rows($resultrccheck); 
      if ($num_rcstock != 0)
      {
      while ($rowrccheck = mysql_fetch_object($resultrccheck))
        {
          $xinclude = "X".$romancount."include";
          $xitemcode = "X".$romancount."itemcode";
          echo "<div><input type='checkbox' name='$xinclude' value='on'  onclick=\"showMe('div$romancount', this)\">
          Include $titletick
          <input type='hidden' name='$xitemcode' value='$romancodetick'>
          <div class='row' id='div$romancount' style='display:none; font-size: 11.2px; color: $color'>
          $titletick cost shown in checkout</div></div>";
          $romancount = $romancount + 1;
        }mysql_free_result($resultrccheck);
      } 
      }
      
    }
If I set the last $$romancount = $romancount + 1; to be like this:

Code: Select all

$romancount = 1;
  while ($romancount <= 12)
    {
    if ($row->{'romancodetick' . $romancount} != NULL)
      {
      $romancodetick = $row->{'romancodetick' . $romancount};
      $titletick = $row->{'titletick' . $romancount};
      
      $resultrccheck = mysql_query ("SELECT rcstock FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'");
      $num_rcstock = mysql_num_rows($resultrccheck); 
      if ($num_rcstock != 0)
      {
      while ($rowrccheck = mysql_fetch_object($resultrccheck))
        {
          $romancountcheck = "in";
          $xinclude = "X".$romancount."include";
          $xitemcode = "X".$romancount."itemcode";
          echo "<div><input type='checkbox' name='$xinclude' value='on'  onclick=\"showMe('div$romancount', this)\">
          Include $titletick
          <input type='hidden' name='$xitemcode' value='$romancodetick'>
          <div class='row' id='div$romancount' style='display:none; font-size: 11.2px; color: $color'>
          $titletick cost shown in checkout</div></div>";
        }mysql_free_result($resultrccheck);
      } 
      }
      $romancount = $romancount + 1;
    }
It works, but I only want it to count "up" if something is found in stock.
It should still loop, but only add one to the count, if it is in stock. So I don't see why putting that in the section where something is found to be "in stock" should cause it to crash. I get no error messages. That part of the page just doesn't load.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

Now trying this to see if I can spot the error.
"romancodetick1" has an item "in stock", but romancodetick2 does NOT.
It now goes into a never-ending loop and shows romancodetick1 result over and over.

It's late in the day here, maybe that's why I cannot see where I've missed something....???

Code: Select all

if ($row->titletick1 != NULL)
{
  echo "<font style='font-size: 12px'>";
  if ($row->catid == "507") { $color = "#009900";} else { $color = "#ff5500";}
  $romancount = 1;
  while ($romancount <= 12)
    {
          if ($row->{'romancodetick' . $romancount} != NULL)
            {
            $romancodetick = $row->{'romancodetick' . $romancount};
            $titletick = $row->{'titletick' . $romancount};
            $resultrccheck = mysql_query 
            ("SELECT rcstock FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'");
            $num_rcstock = mysql_num_rows($resultrccheck); 
            if ($num_rcstock != 0)
              {
              while ($rowrccheck = mysql_fetch_object($resultrccheck))
                {
                  $romanstockcheck = "yes";
                  $xinclude = "X".$romancount."include";
                  $xitemcode = "X".$romancount."itemcode";
                  echo "<div><input type='checkbox' name='$xinclude' value='on'  onclick=\"showMe('div$romancount', this)\">
                  Include $titletick
                  <input type='hidden' name='$xitemcode' value='$romancodetick'>
                  <div class='row' id='div$romancount' style='display:none; font-size: 11.2px; color: $color'>
                  $titletick cost shown in checkout</div></div>";
                } mysql_free_result($resultrccheck);
              }
            }
      if (romanstockcheck == "yes") { $romancount = $romancount + 1; }
      echo "$romancount, $romancodetick";
    }
    echo "</font>";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can you add a count digit to the end of a $row->variable

Post by Christopher »

simonmlewis wrote:So how would do you the above that I have done, in an array, as I don't think your first array was the answer. With respect to a fellow coder!
I am baffled by this code. We have had a couple of posts computing database field names recently. It is certainly a sign that you should be using a link table rather than these numbered fields.

Beyond that ... why are you using mysql_fetch_object() and not mysql_fetch_assoc() ? Fetching associative arrays would simplify this problem -- it is one of the things that PHP does well. And why are you looping and fetching rows (as a $rowrccheck object) and not even using them?!? Or looping while($romancount <= 12) but never decrementing $romancount in the loop?!? What is this code supposed to be doing?
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you add a count digit to the end of a $row->variable

Post by simonmlewis »

This is it at the moment. It's going thru the loop, but it's still counting up from 1 at the bottom, when it shouldn't be.
The system now should be showing an "in stock" product for 1, out of stock for 2 (so NOT counting up), in stock for 3, and out of stock for 4 (so NOT counting up).

It's to show tick boxes on a product page when the product code shows it's in stock.
If I put the "++" code after this: $titletick cost shown in checkout</div></div>";
ie.

Code: Select all

$titletick cost shown in checkout</div></div>"; $romancount ++;
It throws a fit, and gives me a million 222222222222222222222222222 on the screen.

If I ask it to echo <font color='#ff0000'>$romancount</font> at the bottom, I get a 'yes' even if I shouldn't. I did spot that the previous "yes" will be remembered, so I set it to NULL at the end, but then that products the 222222222222222222222222222.

So am a little lost.

Your questions of why am I doing certain ways? Answer: because that'sthe method what I know.

Code: Select all

if ($row->titletick1 != NULL)
{
  echo "<br/><font style='font-size: 12px'>";
  if ($row->catid == "507") { $color = "#009900";} else { $color = "#ff5500";}
  $romancount = 1;
  while ($romancount <= 12)
    {
    
          if ($row->{'romancodetick' . $romancount} != NULL)
            {
            $romancodetick = $row->{'romancodetick' . $romancount};
            $titletick = $row->{'titletick' . $romancount};
            $resultrccheck = mysql_query ("SELECT rcstock FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'");
            $num_rcstock = mysql_num_rows($resultrccheck); 
            if ($num_rcstock == 1)
              {
                  $romanstockcheck = "yes";
                  $xinclude = "X".$romancount."include";
                  $xitemcode = "X".$romancount."itemcode";
                  echo "<div><input type='checkbox' name='$xinclude' value='on'  onclick=\"showMe('div$romancount', this)\">
                  Include $titletick $romanstockcheck<font color='#009900'>$romancount</font>
                  <input type='hidden' name='$xitemcode' value='$romancodetick'>
                  <div class='row' id='div$romancount' style='display:none; font-size: 11.2px; color: $color'>
                  $titletick cost shown in checkout</div></div>";
              }
            }
            
      if ($romanstockcheck == "yes") 
      { $romancount ++; }
      echo "<font color='#ff0000'>$romancount</font>";
    }
    echo "</font>";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can you add a count digit to the end of a $row->variable

Post by Christopher »

simonmlewis wrote:

Code: Select all

$titletick cost shown in checkout</div></div>"; $romancount ++;
It throws a fit, and gives me a million 222222222222222222222222222 on the screen.

If I ask it to echo <font color='#ff0000'>$romancount</font> at the bottom, I get a 'yes' even if I shouldn't. I did spot that the previous "yes" will be remembered, so I set it to NULL at the end, but then that products the 222222222222222222222222222.
The reason it does that your while() control logic is spread out in several places. I would recommend a for() loop from 1..12 to deal with each field in $row.Then only echo if the field is not null and there is data.
simonmlewis wrote:So am a little lost.

Your questions of why am I doing certain ways? Answer: because that'sthe method what I know.
We're glad to help. I would recommend fetching assoc arrays instead of objects unless you actually need objects.

It also helps to separate where you used PHP as a programming language from where you use PHP as a templating language. Mixing them is the basic way to do PHP, but as your code gets more complicated the separation makes things clearer. So in this case, have a separate function/method that get the row of data. Pass the data to a separate function/method that produces the HTML. If you post a little more of the code I can show you how it might be refactored.
(#10850)
Post Reply