• 0

[PHP] if row { } ?


Question

I have a php/mysql query that returns 4 rows and outputs the data inside a table.

I want insert something in the middle of it that does something along the lines of 'if the row = row1 then { do something } elseif the row = row 4 then { do something }'

I can't work out how it should be done, can anyone help?


                                $thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM portfolio ORDER BY id DESC");
				if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

				while($results = mysql_fetch_array($thumbs)) { 
				//display data
				}

Link to comment
Share on other sites

20 answers to this question

Recommended Posts

  • 0

I haven't used PHP for ages so there may be a better way to do this but you could do:

                                $thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM portfolio ORDER BY id DESC");
                                if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

                                for ($i = 1; $results = mysql_fetch_array($thumbs); $i++)
                                {
                                //display data
                                    if ($i == 1) { ... }
                                    if ($i == mysql_num_rows($thumbs)) { ... }
                                    ....
                                }

If it's always 4 rows you can just compare it to 4 instead of the mysql_num_rows.

Link to comment
Share on other sites

  • 0

thanks for you input i come up with this below, i'm not sure if its right but different to yours. i think mine is still just counting the number of rows?


$numrows = mysql_num_rows($result);

				while($results = mysql_fetch_array($thumbs)) { 

					echo "<div class=\"entry\"";
					if ($numrows==1) {
						echo " style=\"margin-left:0px;\"";
					} elseif($numrows==4) {
						echo " style=\"margin-right:0px;\"";
					}

Link to comment
Share on other sites

  • 0

my full code using your code

$thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC LIMIT 0, 4");
				if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

				for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {

					echo "<div class=\"entry\"";
					if ($i == 1) {
						echo " style=\"margin-left:0px;\"";
					} elseif ($i == mysql_num_rows($thumbs)) {
						echo " style=\"margin-rights:0px;\"";
					}
					echo ">\n<a href=\"portfolio/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"portfolio/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
					echo "<h4>" . $results['client'] . "</h4>\n";
					echo "<h6>" . $results['role'] . "</h6>\n</div>";				}

Link to comment
Share on other sites

  • 0

What you're looking for is the modulus. ;)

<?php
foreach(range(1, 100) as $index){
 if(0 === $index % 4){
	printf("%d is divisible by 4<br />\n", $index);
 }
}
/*
 4 is divisible by 4
 8 is divisible by 4
 12 is divisible by 4
 16 is divisible by 4
 20 is divisible by 4
 24 is divisible by 4
 28 is divisible by 4
 32 is divisible by 4
 36 is divisible by 4
 40 is divisible by 4
 44 is divisible by 4
 48 is divisible by 4
 52 is divisible by 4
 56 is divisible by 4
 60 is divisible by 4
 64 is divisible by 4
 68 is divisible by 4
 72 is divisible by 4
 76 is divisible by 4
 80 is divisible by 4
 84 is divisible by 4
 88 is divisible by 4
 92 is divisible by 4
 96 is divisible by 4
 100 is divisible by 4
*/

Link to comment
Share on other sites

  • 0

OK so i can do it when it's divided by 4. but what about 1,5,9,13,17..

You could just flag something when its divisible by 4, so the next time it enters the loop, it'll do something special, along the lines of:

<?php
$next = 0;
foreach(range(1, 100) as $index){
 if(0 === $index % 4){
        printf("%d is divisible by 4<br />\n", $index);
        $next = 1;
}
        if($next == 1){
             //Do what you need to do here
             $next = 0;
        }
}
/*
 4 is divisible by 4, so we flag $next so on the next iteration, itll do what you need. (On 5)
 8 is divisible by 4, so we flag $next so on the next iteration, itll do what you need. (On 9)
 12 is divisible by 4, so we flag $next so on the next iteration, itll do what you need. (On 13)
 16 is divisible by 4, so we flag $next so on the next iteration, itll do what you need. (On 17)
 20 is divisible by 4, so we flag $next so on the next iteration, itll do what you need. (On 21)
 etc...
*/

Or at least that what i would try to do :p.

Link to comment
Share on other sites

  • 0

I cant get it going, its not producing errors but the output is wrong, here is my full code:


<?php 
				$thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
				if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

				for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {

					echo "<div class=\"entry\"";

					$next = 0;

					foreach(range(1, 100) as $i) {

						if ($i == 1) { //for first thumb

							echo " style=\"margin-left:0px;\"";

						} elseif(0 === $i % 4) { //for the last (4th) thumb in every row

							printf(" style=\"margin-right:0px;\"");

							$next = 1;

							if($next == 1) {

								printf(" style=\"margin-left:0px;\""); // for the rest of the 1st thumbs in every row.

             					$next = 0;
        					}

						}
					}

					echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
					echo "<h4>" . $results['client'] . "</h4>\n";
					echo "<h6>" . $results['role'] . "</h6>\n</div>\n";

				} ?>

this is the crazy output lol

<div class="entry" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;"> 

Link to comment
Share on other sites

  • 0

same thing


<?php 
				$thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
				if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

				for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {

					echo "<div class=\"entry\"";

					$next = 0;

					foreach(range(1, 100) as $i) {

						if ($i == 1) {

							echo " style=\"margin-left:0px;\"";

						} else {

                			if(0 === ($i % 4)) {

                        		printf(" style=\"margin-right:0px;\"");
                        		$next = 1;
                			}

                			if($next == 1) {

                       			printf(" style=\"margin-left:0px;\"");
                        		$next = 0;

                			}


						}

						}


					echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
					echo "<h4>" . $results['client'] . "</h4>\n";
					echo "<h6>" . $results['role'] . "</h6>\n</div>\n";

				} ?>

Link to comment
Share on other sites

  • 0

See my comment in code.

I cant get it going, its not producing errors but the output is wrong, here is my full code:


<?php 
				$thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
				if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

				for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {

					echo "<div class=\"entry\"";

					$next = 0;

					foreach(range(1, 100) as $i) {<======================= You are asking it to print the margin 100 times. why?

						if ($i == 1) { //for first thumb

							echo " style=\"margin-left:0px;\"";

						} elseif(0 === $i % 4) { //for the last (4th) thumb in every row

							printf(" style=\"margin-right:0px;\"");

							$next = 1;

							if($next == 1) {

								printf(" style=\"margin-left:0px;\""); // for the rest of the 1st thumbs in every row.

             					$next = 0;
        					}

						}
					}

					echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
					echo "<h4>" . $results['client'] . "</h4>\n";
					echo "<h6>" . $results['role'] . "</h6>\n</div>\n";

				} ?>

this is the crazy output lol

<div class="entry" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;" style="margin-right:0px;" style="margin-left:0px;"> 

Link to comment
Share on other sites

  • 0

The foreach / range bit was just AnthonySterling showing you an example of how modulus works, you weren't supposed to include the whole example in your code :p

<?php 
                                $thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
                                if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

                                for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {
                                        echo '<div class="entry"';
					if ( (($i - 1) % 4) === 0) 
					        echo ' style="margin-left:0px;"'; //first thumb in row
					else if ( ($i % 4) === 0) 
					        echo ' style="margin-right:0px;"'; // last thumb in row

                                        echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
                                        echo "<h4>" . $results['client'] . "</h4>\n";
                                        echo "<h6>" . $results['role'] . "</h6>\n</div>\n";                                        
                                } ?>

Link to comment
Share on other sites

  • 0

I see why there was a foreach loop there :).

The foreach / range bit was just AnthonySterling showing you an example of how modulus works, you weren't supposed to include the whole example in your code :p

<?php 
                                $thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
                                if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

                                for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {
                                        echo '<div class="entry"';
					if ( (($i - 1) % 4) === 0) 
					        echo ' style="margin-left:0px;"'; //first thumb in row
					else if ( ($i % 4) === 0) 
					        echo ' style="margin-right:0px;"'; // last thumb in row

                                        echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
                                        echo "<h4>" . $results['client'] . "</h4>\n";
                                        echo "<h6>" . $results['role'] . "</h6>\n</div>\n";                                        
                                } ?>

Link to comment
Share on other sites

  • 0

The foreach / range bit was just AnthonySterling showing you an example of how modulus works, you weren't supposed to include the whole example in your code :p

<?php 
                                $thumbs = mysql_query("SELECT id, category, path, client, img_url, role FROM `portfolio` ORDER BY id DESC");
                                if (!$thumbs) { echo 'Could not run query: ' . mysql_error(); exit; }

                                for ($i = 1; $results = mysql_fetch_array($thumbs); $i++) {
                                        echo '<div class="entry"';
					if ( (($i - 1) % 4) === 0) 
					        echo ' style="margin-left:0px;"'; //first thumb in row
					else if ( ($i % 4) === 0) 
					        echo ' style="margin-right:0px;"'; // last thumb in row

                                        echo ">\n<a href=\"work/" . $results['category'] . "/" . $results['path'] . "\"><img src=\"work/" . $results['category'] . "/" . $results['path'] . "/" . $results['img_url'] . "\" alt=\"" . $results['client'] . "\" /></a>\n";
                                        echo "<h4>" . $results['client'] . "</h4>\n";
                                        echo "<h6>" . $results['role'] . "</h6>\n</div>\n";                                        
                                } ?>

That will certainly work a lot better :p

Link to comment
Share on other sites

This topic is now closed to further replies.