Jump to content

[SOLVED] Make the script stand alone


confused_aswell

Recommended Posts

Hi

 

The script below works fine via the $conn and $db variables, but I need to remove this information so I can add an

require('includes/application_top.php');

and

require(DIR_WS_INCLUDES . 'header.php');

These includes contain all of the connections to the database info and seesions etc. So what I need is to make the script below a stand alone script so that the above can be added to the script and have it still work. I think I need to change a few items here and there, but I would appreciate some professional eyes look at it too.

 

Thanks,

 

Phil

 

<?php 

  
// database connection info   
$conn = mysql_connect('localhost','username','password') or trigger_error("SQL", E_USER_ERROR);   
$db = mysql_select_db('database_name',$conn) or trigger_error("SQL", E_USER_ERROR);   
  
// find out how many rows are in the table    
$sql = "SELECT COUNT(*) FROM zen_orders";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
$r = mysql_fetch_row($result);   
$numrows = $r[0]; ?>
<div align="center">
<?php   
echo "Here is your orders table in your database, you have $numrows entries in your table. Please enter the number you would like to start downloading from?<br/><br/>If you want to download all records in the table enter 0, but if you are downloading from entry number 1000 then enter 999.";?>
<form name="form1" method="post" action="excel1.php">
  <p>
    <input name="limit" type="text" id="limit" size="5">
    <input type="submit" name="Submit" value="Download">
  </p>
</form> 
<?php
// number of rows to show per page   
$rowsperpage = 50;   
// find out total pages   
$totalpages = ceil($numrows / $rowsperpage);   
  
// get the current page or set a default   
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {   
   // cast var as int   
   $currentpage = (int) $_GET['currentpage'];   
} else {   
   // default page num   
   $currentpage = 1;   
} // end if   

// if current page is greater than total pages...   
if ($currentpage > $totalpages) {   
   // set current page to last page   
   $currentpage = $totalpages;   
} // end if   
// if current page is less than first page...   
if ($currentpage < 1) {   
   // set current page to first page   
   $currentpage = 1;   
} // end if   
  
// the offset of the list, based on current page    
$offset = ($currentpage - 1) * $rowsperpage;   
  
// get the info from the db    
$sql = "SELECT zen_orders.orders_id, zen_orders.customers_name, zen_orders.customers_street_address, zen_orders.customers_suburb, zen_orders.customers_city, zen_orders.customers_state, zen_orders.customers_postcode FROM zen_orders LIMIT $offset, $rowsperpage";   
$result = mysql_query($sql, $conn) or trigger_error(mysql_error() . "<br />\n" . $sql, E_USER_ERROR); ?>
</div>

<table align="center" bgcolor="#e8e8e8" bordercolor="#000000" cellspacing="1"  border="1" >
<?php 
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) { ?>  
   
   

<tr><td><?php echo  $list['orders_id'] . " , " . $list['customers_name'] . " , " . $list['customers_street_address'] . " , " . $list['customers_suburb'] . " , " . $list['customers_city'] . " , " . $list['customers_state'] . " , " . $list['customers_postcode'] ."<br />"; 
}  ?></td>
</tr>
</table> 
<div align="center"><br>
<?php  
/******  build the pagination links ******/  
// range of num links to show   
$range = 3;   
  
// if not on page 1, don't show back links   
if ($currentpage > 1) {   
   // show << link to go back to page 1   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";   
   // get previous page num   
   $prevpage = $currentpage - 1;   
   // show < link to go back to 1 page   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";   
} // end if    
  
// loop to show links to range of pages around current page   
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {   
   // if it's a valid page number...   
   if (($x > 0) && ($x <= $totalpages)) {   
      // if we're on current page...   
      if ($x == $currentpage) {   
         // 'highlight' it but don't make a link   
         echo " [<b>$x</b>] ";   
      // if not current page...   
      } else {   
         // make it a link   
     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";   
      } // end else   
   } // end if    
} // end for   
            
// if not on last page, show forward and last page links       
if ($currentpage != $totalpages) {   
   // get next page   
   $nextpage = $currentpage + 1;   
    // echo forward link for next page    
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";   
   // echo forward link for lastpage   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";   
} // end if   
/****** end build pagination links ******/  
?>
</div>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/128994-solved-make-the-script-stand-alone/
Share on other sites

To implement this just change

 

<?php 

  
// database connection info   
$conn = mysql_connect('localhost','username','password') or trigger_error("SQL", E_USER_ERROR);   
$db = mysql_select_db('database_name',$conn) or trigger_error("SQL", E_USER_ERROR);   
  

 

to

<?php 

require('includes/application_top.php');
  

 

Now create the file includes/application_top.php and place the following code

<?php

// database connection info   
$conn = mysql_connect('localhost','username','password') or trigger_error("SQL", E_USER_ERROR);   
$db = mysql_select_db('database_name',$conn) or trigger_error("SQL", E_USER_ERROR);   

?>

Your script should still work as normal.

Hi

 

Thanks for the reply, I have already done that - didn't work! What I have removed the $conn in the $result but it is still throwing up errors.

 

Here are the errors-

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/username/public_html/shopping/admin/view_database.php:2) in /home/username/public_html/shopping/includes/functions/sessions.php on line 108

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/username/public_html/shopping/admin/view_database.php:2) in /home/username/public_html/shopping/includes/functions/sessions.php on line 108

 

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/shopping/admin/view_database.php:2) in /home/username/public_html/shopping/admin/includes/init_includes/init_templates.php on line 36

 

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/shopping/admin/view_database.php:2) in /home/username/public_html/shopping/admin/includes/functions/general.php on line 21

 

All of this is to do with my zencart store.

 

Thanks,

 

Phil

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.