Can I alter the $row->name1 number of a 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 I alter the $row->name1 number of a variable?

Post by simonmlewis »

Code: Select all

		if ($count == 1)
			{
			if ($row->rating1 == "1") { echo " selected='selected'";} 
			}
			if ($count == 1)
			{
			if ($row->rating1 == "2") { echo " selected='selected'";} 
			}
			if ($count == 1)
			{
			if ($row->rating1 == "2") { echo " selected='selected'";} 
			}
			if ($count == 1)
			{
			if ($row->rating1 == "2") { echo " selected='selected'";} 
			}
I am using a count (up to 4) to avoid having too much code in an admin page.
But I need to query four fields in the product table, $row->rating1-4.

I could do it like the above, but there willbe 10 of them.

How can I changing the $row->rating1.... to dynamically query to 1, 2, 3, 4 as the count rises?

So rather than having a ton or queries, I can dynamically change the final character of that variable:
$row->rating1
$row->rating2
$row->rating3
$row->rating4.

Something like $ratingrow = $row->rating[].$count;
This is crap, but kinda what I am trying to get to.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I alter the $row->name1 number of a variable?

Post by Celauran »

Code: Select all

for ($i = 1; $i <= 4; $i++) {
    $prop = 'rating' . $i;
    echo $row->{$prop};
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I alter the $row->name1 number of a variable?

Post by Celauran »

What is it you're actually trying to achieve, though?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can I alter the $row->name1 number of a variable?

Post by requinix »

Variable variables are ugly. I'd change the code to fetch rows in array mode, then you can do the more obvious

Code: Select all

$row['rating' . $i]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I alter the $row->name1 number of a variable?

Post by simonmlewis »

The idea is we tell each category what the four titles are for the ratings of 1-10.
So Shirts might have "quality, value for money, stretch resistance, whiteness" for example.

Then each product edit page will show these titles, and one can select the 1-10 rating from a dropdown.

If I did it a really bad way, I'd have a horrendous amount of code. So am using a $count, so it only happens the four times needed, but in much less code.

The $row->ratings1 is the field name in the database table for products. So I need to query ratings1, 2, 3 and 4, without manually doing it four times. Hence, doing that variable dynamically.

Code: Select all

			<div class='edittitle'>Product ratings:</div>
			<table>
			";
			$count = 1;
			$ratingselect = null;
			while ($count <= 4)
			{
			$ratingtitle = "ratingtitle"."$count";
			$rating = "ratingtitle"."$count";
			$querycat = ("SELECT $ratingtitle AS rowratingtitle FROM categories WHERE id =:catid");
$resultcat = $pdo->prepare($querycat);
$resultcat->execute(array(':catid' => $row->catid));
while ($rowcat = $resultcat->fetch(PDO::FETCH_OBJ)) 
      {
      echo "<tr><Td>$rowcat->rowratingtitle";
      }
			echo "</td><td><select name='$rating'>
			<option value='10'"; 
			if ($count == 1)
			{
			$row{'rating' . $count} = $ratingselect;
			if ($ratingselect == "1") { echo " selected='selected'";} 
			}
			echo ">1</option>
			<option value='20'"; if ($row->rating1 == "2") { echo " selected='selected'";} echo ">2</option>
			<option value='30'"; if ($row->rating1 == "3") { echo " selected='selected'";} echo ">3</option>
			<option value='40'"; if ($row->rating1 == "4") { echo " selected='selected'";} echo ">4</option>
			<option value='50'"; if ($row->rating1 == "5") { echo " selected='selected'";} echo ">5</option>
			<option value='60'"; if ($row->rating1 == "6") { echo " selected='selected'";} echo ">6</option>
			<option value='70'"; if ($row->rating1 == "7") { echo " selected='selected'";} echo ">7</option>
			<option value='80'"; if ($row->rating1 == "8") { echo " selected='selected'";} echo ">8</option>
			<option value='90'"; if ($row->rating1 == "9") { echo " selected='selected'";} echo ">9</option>
			<option value='100'"; if ($row->rating1 == "10") { echo " selected='selected'";} echo ">10</option>
			</select></td></tr>";
			
			$count ++;
			}
		
		echo "</table>
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 I alter the $row->name1 number of a variable?

Post by simonmlewis »

How do I do this:

Code: Select all

$row['rating' . $i]
When I don't do it in that way for the rest of the page, it's like this: $row->rating....
So can it be done with the -> method?

Secondly, I did try the first option, but it echoed nothing on screen:

Code: Select all

<table>
			";
			$count = 1;
			$ratingselect = null;
			while ($count <= 4)
			{
			$ratingprod = 'rating' . $count;
			$ratingproduct =  $row->{$ratingprod};
			$ratingtitle = "ratingtitle"."$count";
			$rating = "ratingtitle"."$count";
			$querycat = ("SELECT $ratingtitle AS rowratingtitle FROM categories WHERE id =:catid");
$resultcat = $pdo->prepare($querycat);
$resultcat->execute(array(':catid' => $row->catid));
while ($rowcat = $resultcat->fetch(PDO::FETCH_OBJ)) 
      {
      echo "<tr><Td>$rowcat->rowratingtitle <b>here: $ratingproduct</b>";
      }
			echo "</td><td><select name='$rating'>
			<option value='10'"; 
			if ($count == 1)
			{
			if ($ratingselect == "1") { echo " selected='selected'";} 
			}
			echo ">1</option>
			<option value='20'"; if ($row->rating1 == "2") { echo " selected='selected'";} echo ">2</option>
			<option value='30'"; if ($row->rating1 == "3") { echo " selected='selected'";} echo ">3</option>
			<option value='40'"; if ($row->rating1 == "4") { echo " selected='selected'";} echo ">4</option>
			<option value='50'"; if ($row->rating1 == "5") { echo " selected='selected'";} echo ">5</option>
			<option value='60'"; if ($row->rating1 == "6") { echo " selected='selected'";} echo ">6</option>
			<option value='70'"; if ($row->rating1 == "7") { echo " selected='selected'";} echo ">7</option>
			<option value='80'"; if ($row->rating1 == "8") { echo " selected='selected'";} echo ">8</option>
			<option value='90'"; if ($row->rating1 == "9") { echo " selected='selected'";} echo ">9</option>
			<option value='100'"; if ($row->rating1 == "10") { echo " selected='selected'";} echo ">10</option>
			</select></td></tr>";
			
			$count ++;
			}
		
		echo "</table>
I've not done it exactly with the 'for' loop, as I am already in a loop, using $count.
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 I alter the $row->name1 number of a variable?

Post by simonmlewis »

Should this not work??

Code: Select all

<table>
			";
			$count = 1;
			$ratingselect = null;
			while ($count <= 4)
			{
			$ratingprod = 'rating' . $count;

    $prod = "rating" . "$count";

			$ratingtitle = "ratingtitle"."$count";
			$rating = "ratingtitle"."$count";
			$querycat = ("SELECT $ratingtitle AS rowratingtitle FROM categories WHERE id =:catid");
$resultcat = $pdo->prepare($querycat);
$resultcat->execute(array(':catid' => $row->catid));
while ($rowcat = $resultcat->fetch(PDO::FETCH_OBJ)) 
      {
      echo "<tr><Td>$rowcat->rowratingtitle";
      }
			echo "</td><td><select name='$rating'>
			<option value='10'"; 
			if ($row->{$prod} == 10)
			{
			if ($ratingselect == "10") { echo " selected='selected'";} 
			}
			echo ">10</option>
			<option value='20'"; if ($row->{$prod} == "2") { echo " selected='selected'";} echo ">2</option>
			<option value='30'"; if ($row->{$prod} == "3") { echo " selected='selected'";} echo ">3</option>
			<option value='40'"; if ($row->{$prod} == "4") { echo " selected='selected'";} echo ">4</option>
			<option value='50'"; if ($row->{$prod} == "5") { echo " selected='selected'";} echo ">5</option>
			<option value='60'"; if ($row->{$prod} == "6") { echo " selected='selected'";} echo ">6</option>
			<option value='70'"; if ($row->{$prod} == "7") { echo " selected='selected'";} echo ">7</option>
			<option value='80'"; if ($row->{$prod} == "8") { echo " selected='selected'";} echo ">8</option>
			<option value='90'"; if ($row->{$prod} == "9") { echo " selected='selected'";} echo ">9</option>
			<option value='100'"; if ($row->{$prod} == "10") { echo " selected='selected'";} echo ">10</option>
			</select></td></tr>";
			
			$count ++;
			}
		
		echo "</table>
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 I alter the $row->name1 number of a variable?

Post by Christopher »

Urgh! This is why mixing Domain and Presentation code is a bad idea. This code is a mess.

1. Separate out you database code, preferably into a Model object or at least a function. It is just fetching the same titles four times and then doing something wacky with the table tags. It is in a while() loop, but is only getting on title?!? Just have it get the title with something sensible like title = $categories->findTitle($row->catid).

2. Create a Helper function/class to generate <option> tags. Then you code will not have to repeat the if selected code ad nauseam.

3. You should be left with your actual Presentation code which is just a PHP template.

You are making programming unnecessarily hard for yourself with this confusing style of coding.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I alter the $row->name1 number of a variable?

Post by simonmlewis »

Getting a Model onject, finTitle functions, Helper functions..... this is all too much.
The whole page works just fine, and does for goodness knows how many of our web sites.
All that is failing is this ONE tiny bit. Get this $row->{$prod} working, and it's fine.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I alter the $row->name1 number of a variable?

Post by Celauran »

Christopher wrote:You are making programming unnecessarily hard for yourself with this confusing style of coding.
A thousand times this. A little OOP goes a long way. Truly.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I alter the $row->name1 number of a variable?

Post by simonmlewis »

I'm sure it does, but I am not about to completely re-write all my code, for one variable that has this issue!
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 I alter the $row->name1 number of a variable?

Post by simonmlewis »

The valued suggesetion ought to work, but it doesn't, and I don't know why.
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 I alter the $row->name1 number of a variable?

Post by Christopher »

simonmlewis wrote:Getting a Model onject, finTitle functions, Helper functions..... this is all too much.
It's actually not very much at all. I really just suggested restructuring your page, even with function, so you could see what is going on. And to simplify generating those <option>'s. You would even need to use classes if you weren't comfortable with them -- just functions.
simonmlewis wrote:The whole page works just fine, and does for goodness knows how many of our web sites.
I am sure it does, but it doesn't seem so easy to figure out why it is going wrong. Well designed code makes changes easier.
simonmlewis wrote:All that is failing is this ONE tiny bit. Get this $row->{$prod} working, and it's fine.
It is really hard to know what is going on in that code because of how it is designed. Your appear to be loading the same row from the database every loop -- in a loop. It is difficult to give you suggestions because I just would not continue down the road the code is going. The fact that you need to append numbers onto field names indicates that the problems go back before this code.
simonmlewis wrote:I'm sure it does, but I am not about to completely re-write all my code, for one variable that has this issue!
Again, hardly a complete re-write. That code could be cleaned up in 5-10 minutes. I would refactor it just for my own sanity.

Maybe return an array instead of an object from the database? At least then the keys are strings and you can build them easier.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I alter the $row->name1 number of a variable?

Post by simonmlewis »

The reason it is doing the same row, is because each category has four columns for ratingtitle1, ratingtitle2...3..and 4.
Then in the Products table, there is rating1, rating2...3..and 4.
So Shirts might have four different ratings titles, than DVDs for example.

But then in the products, you assign each product a rating for each of the four areas.
So I need to say $row->rating1 value is x, $row-rating2 value is x and so on to 4.

Trust me, if I had done this what in my eyes is a bad way, I'd have had 5 times the code!!!

So I just want to sort this variable. Not write the lot in a way I don't fully understand, and then have to relearn stuff... when the site works and the client is happy.
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 I alter the $row->name1 number of a variable?

Post by Christopher »

simonmlewis wrote:The reason it is doing the same row, is because each category has four columns for ratingtitle1, ratingtitle2...3..and 4.
Then in the Products table, there is rating1, rating2...3..and 4.
So Shirts might have four different ratings titles, than DVDs for example.

But then in the products, you assign each product a rating for each of the four areas.
So I need to say $row->rating1 value is x, $row-rating2 value is x and so on to 4.
But in the code above, the value of $row->catid is the same every loop, so the same $rowcat is fetched every loop -- four times. Luckily MySQL caches the query results for you.
simonmlewis wrote:Trust me, if I had done this what in my eyes is a bad way, I'd have had 5 times the code!!!

So I just want to sort this variable. Not write the lot in a way I don't fully understand, and then have to relearn stuff... when the site works and the client is happy.
I am less worried about the client being happy and more about you spending unnecessary time trying to sort out code like this. Time is money as they say.

I think we've given you some ideas on how we'd move toward solving this problem.
(#10850)
Post Reply