7 Replies - 706 Views - Last Post: 23 March 2013 - 04:34 PM Rate Topic: -----

#1 TRANSAMS4EVER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-October 11

While loop question

Posted 21 March 2013 - 03:04 PM

I am querying a SQL database (Not MySQL)the query is looking for past years it outputs four rows (Example row 1 =1998 row 2=2000 row3=2002 row4=2010)


$query2="SELECT  Date FROM table WHERE app_date!='$SQL_today'";
$OUTPUT2= sqlsrv_query($con, $query2, $params, $options);
if ($OUTPUT2)
{
while ($row = sqlsrv_fetch_array($OUTPUT2, SQLSRV_FETCH_ASSOC)){
//get app table info
$years= $row['Date']->format('o');
 echo"$years";
}
}


The echo"$years"; above displays 1998 2000 2002 2010

but if the echo"$years"; is outside of the while it just echos 2010.

Is there any way I can get the echo to be outside of the while loop and still echo all four years?

Is This A Good Question/Topic? 0
  • +

Replies To: While loop question

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: While loop question

Posted 21 March 2013 - 03:06 PM

How about creating an array variable outside the loop and adding each year to the array, then printing outside of the array?
Was This Post Helpful? 1
  • +
  • -

#3 TRANSAMS4EVER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-October 11

Re: While loop question

Posted 21 March 2013 - 03:26 PM

View PostJackOfAllTrades, on 21 March 2013 - 03:06 PM, said:

How about creating an array variable outside the loop and adding each year to the array, then printing outside of the array?

Sounds like a great idea, but i'm not sure how to do this.

$query2="SELECT  Date FROM table WHERE app_date!='$SQL_today'";
$OUTPUT2= sqlsrv_query($con, $query2, $params, $options);
if ($OUTPUT2)
{
while ($row = sqlsrv_fetch_array($OUTPUT2, SQLSRV_FETCH_ASSOC)){
//get app table info

$years= $row['Date']->format('o');
 echo"$years";
 $sql_years=array("$years");
}
}
$arrlength=count($sql_years);
for($x=0;$x<$arrlength;$x++)
  {
  echo $sql_years[$x];
  echo "<br>";
  }



Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: While loop question

Posted 21 March 2013 - 04:07 PM

Take some time to learn PHP.
Was This Post Helpful? 3
  • +
  • -

#5 TRANSAMS4EVER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-October 11

Re: While loop question

Posted 22 March 2013 - 06:43 AM

View PostJackOfAllTrades, on 21 March 2013 - 04:07 PM, said:

Take some time to learn PHP.

Why is everyone on this forum jackasses
Was This Post Helpful? 0
  • +
  • -

#6 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,933
  • Joined: 30-April 10

Re: While loop question

Posted 22 March 2013 - 10:14 AM

This is a learning website. If you want someone to do it for you, your out of luck.
Was This Post Helpful? 0
  • +
  • -

#7 Koyume  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 89
  • Joined: 19-February 13

Re: While loop question

Posted 22 March 2013 - 12:21 PM

View PostJackOfAllTrades, on 21 March 2013 - 05:07 PM, said:

Take some time to learn PHP.


To go with what Jack was saying, here's some reading material (This is of mySQLi, but it works closely the same way. You can look what whatever database software you are using and configure accordingly.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: While loop question

Posted 23 March 2013 - 04:34 PM

The point was fairly missed.

Arrays are the MOST USED type in PHP, and if you don't know them...you're fucked. Or you're just a copy/paste programmer, the bane of my existence.

<?php

function printLine($leadingNewline = false)
{
	if ($leadingNewline)
		print("\n");
	for ($i = 0; $i < 20; ++$i)
		print("=");
	print("\n");
}

$myArray = array();
for ($i = 0; $i < 10; ++$i)
{
    $myArray[] = $i * 2;
} 

print_r($myArray);
printLine(true);

var_dump($myArray);
printLine(true);

var_export($myArray);
printLine(true);

print(implode(', ', $myArray));
printLine(true);

foreach($myArray as $entry)
	print("$entry\n");
printLine();

for ($i = 0; $i < count($myArray); ++$i)
{
	print("{$myArray[$i]} ");
}
print("\n");

?>


Output:

$ php -f lazy.php 
Array
(
    [0] => 0
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 12
    [7] => 14
    [8] => 16
    [9] => 18
)

====================
array(10) {
  [0]=>
  int(0)
  [1]=>
  int(2)
  [2]=>
  int(4)
  [3]=>
  int(6)
  [4]=>
  int(8)
  [5]=>
  int(10)
  [6]=>
  int(12)
  [7]=>
  int(14)
  [8]=>
  int(16)
  [9]=>
  int(18)
}

====================
array (
  0 => 0,
  1 => 2,
  2 => 4,
  3 => 6,
  4 => 8,
  5 => 10,
  6 => 12,
  7 => 14,
  8 => 16,
  9 => 18,
)
====================
0, 2, 4, 6, 8, 10, 12, 14, 16, 18
====================
0
2
4
6
8
10
12
14
16
18
====================
0 2 4 6 8 10 12 14 16 18 

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1