Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to pull out the whole column for row[3] in the following script. Everything works, but unfortunately, the code is only pulling out the last row's data from the whole page while everything else is lost. I have never converted a whole array from datetime to string, and I am unsure of what I'm doing wrong. I would appreciate any help with this:

<?php ini_set('display_errors', 1); error_reporting(E_ALL); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" 

src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-    ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"        

rel="stylesheet" type="text/css"/>



</head>


<body>  


<table align="center">
<form id="form1" name="form1" method="get" action="test.php">
<td><label for="from">From</label>
<input name="from" type="text" id="from" size="10"  name='from' value="" /></td>

<td><label for="from">To</label>
<input name="to" type="text" id="to" size="10" name='to'  value="" /></td>
</select>
<td><input type="submit"  id="button" value="Filter" /></td>
</label>

<td>  <a href="test.php"> 
reset</a>
</td>
</table>
</form>


<script>
$(function() {
    var dates = $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selectedDate ) {
            var option = this.id == "from" ? "minDate" : "maxDate",
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });
});
</script>

<?php

function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_ABSOLUTE, 

$offset + $i)) && $i < $rowsPerPage)
{
    array_push($rows, $row);
    $i++;
}
return $rows;
}

// Set the number of rows to be returned on a page.
$rowsPerPage = 30;

// Connect to the server.
$serverName = 'test';
$connOptions = array("Database"=>"test");
$conn = sqlsrv_connect($serverName, $connOptions);
if (!$conn)
die( print_r( sqlsrv_errors(), true));

// Define and execute the query.  
// Note that the query is executed with a "scrollable" cursor.

$date1 = $_GET['from'];
$date2 = $_GET['to'];



$sql = "SELECT * from test WHERE     

(([testtime]   

>= '".$date1."') AND ([testtime] <= '".$date2."')) ";


$stmt = sqlsrv_query($conn, $sql, array(), array( "Scrollable" => 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));

// Display the selected page of data.
echo "<table border='1px' align='center' width = '480'>";
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);

foreach($page as $row)

$result[3] = $row[3]->format('Y-m-d H:i:s');


echo "<tr><td width='65'>$row[0]</td><td width='65'>$row[1]</td><td width='65'>$row[2]     
</td><td width='65'>$result[3]</td></tr>";

echo "</table>";
?>
</table>

<table align='center'>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{     
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
$renderedEllipses = false;
for($i = 1; $i<=$numOfPages; $i++)
{
//If you always want to render pages 1 - 3
if($i < 4) {
    //render link
            $pageLink = "?pageNum=$i&from=$date1&to=$date2";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
}

//If you always want to render current page number
else if($i == $pageNum) {
    //render link
            $pageLink = "?pageNum=$i&from=$date1&to=$date2";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
    //reset ellipses
    $renderedEllipses = false;
}

        //if you always want the last page number
else if(abs($i - $pageNum) == 1) {
    $pageLink = "?pageNum=$i&from=$date1&to=$date2";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
}

            //if you want the middle number
else if ($i ==  round($numOfPages / 2)  ) {
    $pageLink = "?pageNum=$i&from=$date1&to=$date2";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
     //reset ellipses
    $renderedEllipses = false;
}

//if you always want the last page number
else if ($i > $numOfPages - 3 ) {
    //render link
            $pageLink = "?pageNum=$i&from=$date1&to=$date2";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
}

//make sure you only do this once per ellipses group
else {
  if (!$renderedEllipses){
      print("...");
      $renderedEllipses = true;
  }
}
}}
?>

</table>

<table align="center">
<td>PAGE <?php echo $pageNum; ?> of <?php echo $numOfPages; ?> (<?php echo 

$rowsReturned;>         

items)    
<?php // Display Previous Page link if applicable.
if($pageNum > 1)
{
$prevPageLink = "?pageNum=".($pageNum - 1)."&from=$date1&to=$date2";
echo "<a href='$prevPageLink'>Prev</a>";
}

// Display Next Page link if applicable.
if($pageNum < $numOfPages)
{ 
$nextPageLink = "?pageNum=".($pageNum + 1)."&from=$date1&to=$date2";
echo "&nbsp;&nbsp;<a href='$nextPageLink'>Next</a>";
} 



sqlsrv_close( $conn );

?>
</br>

</body>
</html>

If there's any other info I can provide, please let me know.

@JohnMark13 - I am including you as you've helped a lot with the previous answer. Thanks for directing me to the correct area to post. :)

share|improve this question
BTW, Code Review is more for reviewing code and not for fixing something that isn't working. this is kind of borderline, personally I would have posted this on StackOverflow. – Malachi Sep 5 at 15:44
Malachi, I was unsure where to put this so I did post on both, if advised I can remove from here, thanks. @Malachi – phpyesphp Sep 5 at 16:54
we should have flagged it for migration. – Malachi Sep 13 at 18:06

1 Answer

have you tried putting some brackets around what you want inside the foreach loop?

and second

foreach($page as $row)

$result[3] = $row[3]->format('Y-m-d H:i:s');

you are giving the indexed location of the $row[3] which shouldn't really be an Array from what I have glanced at so far. and $result[3] is referencing one location.

I am thinking that you want an increment variable created outside the foreach like

echo "<table border='1px' align='center' width = '480'>";
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);

$j = 0;  //assuming that PHP is Zero Based, if not this could cause issues.

echo "<tr>"

foreach($page as $row)
{
    $result[$j] = $row->format('Y-m-d H:i:s');
    echo "<td width='65'>" + $result[$j] + "</td>"
    $j++
}

also not sure if you wanted the Echo inside the foreach or not not certain but I think that should give you what you are looking for.

which could also be written like

echo "<table border='1px' align='center' width = '480'>";
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);

echo "<tr>"

foreach($page as $row)
{
    //$row->format('Y-m-d H:i:s');
    echo "<td width='65'>" + $row->format('Y-m-d H:i:s') + "</td>"
}

Unless you are using that result elsewhere, then you would want to use the first set of code I gave.

please Test, as PHP is not my Strong Language, I only Dabble with PHP when I have to. so some of my code could be wrong.

Edited Code I have edited my code a little bit. I might have the string concatenation wrong, like I said I am not well versed in PHP but I think I made it clear what i was trying to do there.

more than one row per page

if you have more than one $row per $page then you would need a second foreach loop to cycle through each row of each page

foreach($page as $row)
{
    //$row->format('Y-m-d H:i:s');
    foreach($row as $insideRow)
    {
        echo "<td width='65'>" + $insideRow->format('Y-m-d H:i:s') + "</td>"
    }
}

I think that might give you what you are looking for, or set you on the right path.

share|improve this answer
I have tried your suggestions unfortunately it will error out, also I am quite close to solution as it will bring back date but it is so strange it only gives back the last values in the row for the page so the page should have 30 rows of data and there are 300 pages of data but how it is now it will only show one result for each page and it would be the last row that would show for each page. @Malachi – phpyesphp Sep 5 at 16:57
Guys, I ran into my own solution through multiple tries: echo "<tr><td width='65'>$row[0]</td><td width='65'>$row[1]</td><td width='65'>$row[2]</td><td width='65'>$row[3]</td><td width='150'>".$row[4]- >format('Y-m-d H:i:s')."</td></tr>"; @Malachi you where really close and helped me find my answer so would like to give credit where it is due :). – phpyesphp Sep 5 at 17:06
give credit by upvoting and then if you found an answer, please post it and select your answer as the answer – Malachi Sep 5 at 17:59
1  
Not enough rep to vote up but you get the idea bud, thanks @Malachi – phpyesphp Sep 5 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.