Re-Order Values in PHP Data loops.

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

Post Reply
yellowbear
Forum Newbie
Posts: 3
Joined: Mon Apr 02, 2012 9:11 am

Re-Order Values in PHP Data loops.

Post by yellowbear »

I need some good PHP programming help with my little problem I am having. I have a MySQL table that has 5 columns plus a ID column and thousands and thousands of rows of data.

Please look at the image I have attached
Image

I have created a PHP loop that pulls the information from the table database but I need the columns to go in order, I can not change the order of the MySQL database.

Code: Select all

<?
	$SQLv = "SELECT * FROM tblvalues;";
		if ($objDb->query($SQLv) == true)
		{
		$iCount = $objDb->getCount( );
			for ($i = 0; $i < $iCount; $i ++)
			{
				$Id   			= $objDb->getField($i, "Id");

				$c1   		= $objDb->getField($i, "c1");
				$c2   		= $objDb->getField($i, "c2");
				$c3   		= $objDb->getField($i, "c3");
				$c4   		= $objDb->getField($i, "c4");
				$c5   		= $objDb->getField($i, "c5");
			
			//I need the code to go here (look at the table image ID row 1) and below is the example
			// $value1 = "2";		$c2
			// $value2 = "4";		$c5
			// $value3 = "20";		$c4
			// $value4 = "22";		$c3
			// $value5 = "23";		$c1

?>

	<?= $value1; ?>
	<?= $value2; ?>
	<?= $value3; ?>
	<?= $value4; ?>
	<?= $value5; ?>

<? }  } ?>
Is there away to do this so every row comes out in order and the all have a string attached to each value....
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Re-Order Values in PHP Data loops.

Post by Celauran »

User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Re-Order Values in PHP Data loops.

Post by twinedev »

gugi77 wrote:Hi yellowbear,

Use 'order by' sql syntax:

Code: Select all

$SQLv = "SELECT * FROM tblvalues order by C1"; // it will sort C1 columns ascending
...
$SQLv = "SELECT * FROM tblvalues order by C2 desc"; // it will sort C2 columns descending
This does NOT do what he asked, he is not wanting the rows sorted, he wants the values from the columns sorted for display.

Here is an example:

Code: Select all

$iCount = $objDb->getCount( );
for ($i = 0; $i < $iCount; $i ++)
{
    $aryRowData = array();
    $Id = $objDb->getField($i, "Id");
    $aryRowData[] = $objDb->getField($i, "c1");
    $aryRowData[] = $objDb->getField($i, "c2");
    $aryRowData[] = $objDb->getField($i, "c3");
    $aryRowData[] = $objDb->getField($i, "c4");
    $aryRowData[] = $objDb->getField($i, "c5");

    sort($aryRowData);
		
    ?>

        <?= $aryRowData[0]; ?>
        <?= $aryRowData[1]; ?>
        <?= $aryRowData[2]; ?>
        <?= $aryRowData[3]; ?>
        <?= $aryRowData[4]; ?>

<? }  ?>
yellowbear
Forum Newbie
Posts: 3
Joined: Mon Apr 02, 2012 9:11 am

Re: Re-Order Values in PHP Data loops.

Post by yellowbear »

Thank you very much twinedev. Yes that is what I asked for I'm just trying to implement it, not having any look yet with it but this seams to be the right path to go down. Once again thanks
yellowbear
Forum Newbie
Posts: 3
Joined: Mon Apr 02, 2012 9:11 am

Re: Re-Order Values in PHP Data loops.

Post by yellowbear »

It work fine thank you.

Code: Select all


<?
        $SQLv = "SELECT * FROM tblvalues;";
                if ($objDb->query($SQLv) == true)
                {
                $iCount = $objDb->getCount( );
                        for ($i = 0; $i < $iCount; $i ++)
                        {
																
							$aryRowData = array();
							
							$Id = $objDb->getField($i, "Id");
							$aryRowData[] = $objDb->getField($i, "c1");
							$aryRowData[] = $objDb->getField($i, "c2");
							$aryRowData[] = $objDb->getField($i, "c3");
							$aryRowData[] = $objDb->getField($i, "c4");
							$aryRowData[] = $objDb->getField($i, "c5");
							
							$date   	= $objDb->getField($i, "date");
						
							sort($aryRowData);
															
				?>
               ( 
        <?= $aryRowData[0]; ?>|
        <?= $aryRowData[1]; ?>|
        <?= $aryRowData[2]; ?>|
        <?= $aryRowData[3]; ?>|
        <?= $aryRowData[4]; ?>
        		)<br />
				<? } } ?>

Post Reply