News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

First PHP script - SSI last attachments made.

Started by JayBachatero, June 01, 2005, 06:19:20 PM

Previous topic - Next topic

JayBachatero

im a noobie in programming but i like to mess around and learn new things so i got myself a book and created this from tutorials and other stuff.  what  it does is it connect to my forum database and pulls out the attachments and gives a download link.  the pagination i got it from www.phpfreaks.com.  what i want to do is make this into a SSI function for my board with more capabilities i want to make them sort.  also i want to hide the download location make it a dynamiic link to mask the dir.  you can take a look at the example running on http://kevmundial.com/test_download.php user/pass test/test.  also i know the code need some cleaning can anyone help me do this.

<?php
//kevmundial board downloads

require('/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/connect.php');
require(
'/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/board/SSI.php');
$_SESSION['login_url'] = 'http://kevmundial.com' $_SERVER['PHP_SELF'] . '?' $_SERVER['QUERY_STRING'];
$_SESSION['logout_url'] = 'http://kevmundial.com' $_SERVER['PHP_SELF'] . '?' $_SERVER['QUERY_STRING'];

/* specify path containing files */
$download_path 'board/attachments/';

//conect to database
$con db_connect();

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    
$page test_download.php;
} else {
    
$page $_GET['page'];
}

// Define the number of results per page
$max_results 50;

// Figure out the limit for the query based
// on the current page number.
$from = (($page $max_results) - $max_results);

//select the table
$request mysql_query("SELECT * FROM smf_attachments ORDER BY ID_ATTACH DESC LIMIT $from$max_results");

if (
$context['user']['is_guest'])
{
  echo 
'cant download';
ssi_login();
}
else
  {
  echo 
'<table border=1 align=center>';
    echo 
'  <tr>';
    echo 
'    <td>Song</td>';
    echo 
'    <td>File Size</td>';
    echo 
'    <td>Downloads</td>';
    echo 
'  </tr>';
  while (
$row mysql_fetch_array($request)) {
    echo 
'  <tr>';
    echo 
'    <td>' $row['filename'] . '</td>';
    echo 
'    <td><a href="'$download_path'',$row['filename'], '">download</a> (' $row['size'] . ')</td>';
    echo 
'    <td>' $row['downloads'] . '</td>';
    echo 
'  </tr>';
  }
  echo 
'</table>';
    echo 
'<a onclick="return log_out()" href="'$scripturl'?action=logout;sesc='$context['session_id'], '">logout</a>';


// Figure out the total number of results in DB:
$total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM smf_attachments"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages ceil($total_results $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page 1){
    
$prev = ($page 1);
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for(
$i 1$i <= $total_pages$i++){
    if((
$page) == $i){
        echo 
"$i ";
        } else {
            echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page $total_pages){
    
$next = ($page 1);
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo 
"</center>";
}
mysql_free_result($request);
?>


p.s.  i tried to do my own SSI function a little different from the code above


function ssi_downloads()
{
         global $bd_prefix;
         if ($context['user']['is_guest'])
         {
           echo 'cant download';
         }
         $request = mysql_query("SELECT * FROM {$db_prefix}attachments ORDER BY ID_ATTACH DESC LIMIT 5,5");
         else
           {
             echo '<table border=1 align=center>';
               while ($row = mysql_fetch_array($request))
               {
                   echo '  <tr>';
                   echo '    <td>" . $row['filename'] . "</td>';
                   echo '    <td><a href=\"$download_path" . $row['filename'] . "\">download</a> (" . $row['size'] . ")</td>';
                   echo '  </tr>';
               }
               echo '</table>';
         }
}
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

morph

#1
Quote from: LiL_J on June 01, 2005, 06:19:20 PM
im a noobie in programming but i like to mess around and learn new things so i got myself a book and created this from tutorials and other stuff.  what  it does is it connect to my forum database and pulls out the attachments and gives a download link.  the pagination i got it from www.phpfreaks.com.  what i want to do is make this into a SSI function for my board with more capabilities i want to make them sort.  also i want to hide the download location make it a dynamiic link to mask the dir.  you can take a look at the example running on http://kevmundial.com/test_download.php user/pass test/test.  also i know the code need some cleaning can anyone help me do this.

<?php
//kevmundial board downloads

require('/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/connect.php');
require(
'/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/board/SSI.php');
$_SESSION['login_url'] = 'http://kevmundial.com' $_SERVER['PHP_SELF'] . '?' $_SERVER['QUERY_STRING'];
$_SESSION['logout_url'] = 'http://kevmundial.com' $_SERVER['PHP_SELF'] . '?' $_SERVER['QUERY_STRING'];

/* specify path containing files */
$download_path 'board/attachments/';

//conect to database
$con db_connect();

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    
$page test_download.php;
} else {
    
$page $_GET['page'];
}

// Define the number of results per page
$max_results 50;

// Figure out the limit for the query based
// on the current page number.
$from = (($page $max_results) - $max_results);

//select the table
$request mysql_query("SELECT * FROM smf_attachments ORDER BY ID_ATTACH DESC LIMIT $from$max_results");

if (
$context['user']['is_guest'])
{
  echo 
'cant download';
ssi_login();
}
else
  {
  echo 
'<table border=1 align=center>';
    echo 
'  <tr>';
    echo 
'    <td>Song</td>';
    echo 
'    <td>File Size</td>';
    echo 
'    <td>Downloads</td>';
    echo 
'  </tr>';
  while (
$row mysql_fetch_array($request)) {
    echo 
'  <tr>';
    echo 
'    <td>' $row['filename'] . '</td>';
    echo 
'    <td><a href="'$download_path'',$row['filename'], '">download</a> (' $row['size'] . ')</td>';
    echo 
'    <td>' $row['downloads'] . '</td>';
    echo 
'  </tr>';
  }
  echo 
'</table>';
    echo 
'<a onclick="return log_out()" href="'$scripturl'?action=logout;sesc='$context['session_id'], '">logout</a>';


// Figure out the total number of results in DB:
$total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM smf_attachments"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages ceil($total_results $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page 1){
    
$prev = ($page 1);
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for(
$i 1$i <= $total_pages$i++){
    if((
$page) == $i){
        echo 
"$i ";
        } else {
            echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page $total_pages){
    
$next = ($page 1);
    echo 
"<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo 
"</center>";
}
mysql_free_result($request);
?>


p.s.  i tried to do my own SSI function a little different from the code above


function ssi_downloads()
{
         global $bd_prefix;
         if ($context['user']['is_guest'])
         {
           echo 'cant download';
         }
         $request = mysql_query("SELECT * FROM {$db_prefix}attachments ORDER BY ID_ATTACH DESC LIMIT 5,5");
         else
           {
             echo '<table border=1 align=center>';
               while ($row = mysql_fetch_array($request))
               {
                   echo '  <tr>';
                   echo '    <td>" . $row['filename'] . "</td>';
                   echo '    <td><a href=\"$download_path" . $row['filename'] . "\">download</a> (" . $row['size'] . ")</td>';
                   echo '  </tr>';
               }
               echo '</table>';
         }
}


For the page it self i cleaned it up to how i usualy write everything and came up with this:

<?php
//kevmundial board downloads

require_once('/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/connect.php');
require_once(
'/usr/local/psa/home/vhosts/kevmundial.com/httpdocs/board/SSI.php');
$_SESSION['login_url'] = 'http://kevmundial.com'$_SERVER['PHP_SELF'], '?'$_SERVER['QUERY_STRING'];
$_SESSION['logout_url'] = $_SESSION['login_url'];

/* specify path containing files */
$download_path 'board/attachments/';

//conect to database
$con db_connect();

// If current page number, use it
// if not, set one!

$page = !isset($_GET['page']) ? : (int) $_GET['page'];

// Define the number of results per page
$max_results 50;

// Figure out the limit for the query based
// on the current page number.
$from = (($page $max_results) - $max_results);

if (
$user_info['is_guest'])
{
echo 'You are not allowed to view downloads, please login..<br />';
ssi_login();
die;
}

//select the table. Edit by morph: Added after is_guest check
$request mysql_query("SELECT * FROM {$db_prefix}attachments ORDER BY `ID_ATTACH` DESC LIMIT $from$max_results;");

if (!
$context['user']['is_guest'])
{
  echo 
'
<table align="center" border="1" bordercolor="#000000">
<tr>
<th>Song</th>
<th>File Size</th>
<th>Downloads</th>
</tr>'
;
while ($row mysql_fetch_array($request)) 
{
echo '
<tr>
<td>'
$row['filename'], '</td>
<td><a href="'
$download_path''$row['filename'], '">download</a> ('$row['size'], ' B)</td>
<td>'
$row['downloads'], '</td>
</tr>'
;
}
  echo 
'
</table>'
;
    
ssi_logout($_SESSION['logout_url']);
}


// Figure out the total number of results in DB:
$total_results mysql_num_rows($request);

// Figure out the total number of pages. Always round up using ceil()
$total_pages ceil($total_results $max_results);

// Build Page Number Hyperlinks
echo '<center><small><b>Select a Page</b></small><br />';

// Build Previous Link
if ($page 1)
    echo 
'<a href="'$_SERVER['PHP_SELF'], '?page=', ($page 1), '"><<Previous</a> ';

for (
$i 1$i <= $total_pages$i++){
if ($page == $i)
echo '<b>'$i'</b> ';
else
echo '<a href="'$_SERVER['PHP_SELF'], '?page='$i'">'$i'</a> ';
    }
}

// Build Next Link
if ($page $total_pages)
    echo 
'<a href="'$_SERVER['PHP_SELF'], '?page=', ($page 1), '">Next>></a>';

echo 
'</center>';
}

mysql_free_result($request);

?>


Then for the SSI function i came up with this:

<?php

function ssi_downloads($start 0$limit 5// When putting in file.. ssi_downloads(0, 15); where 0 is the starting row and 15 is the limit
{
global $db_prefix;

if ($user_info['is_guest'])
{
echo 'You are not allowed to view downloads, please login..<br />';
ssi_login();
return;
}

//select the table. Edit by morph: Added after is_guest check
$request mysql_query("SELECT * FROM {$db_prefix}attachments ORDER BY `ID_ATTACH` DESC LIMIT $start$limit;");

if (!$context['user']['is_guest'])
{
  echo '
<table align="center" border="1" bordercolor="#000000">
<tr>
<th>Song</th>
<th>File Size</th>
<th>Downloads</th>
</tr>'
;
while ($row mysql_fetch_array($request)) 
{
echo '
<tr>
<td>'
$row['filename'], '</td>
<td><a href="'
$download_path''$row['filename'], '">download</a> ('$row['size'], ' B)</td>
<td>'
$row['downloads'], '</td>
</tr>'
;
}
  echo '
</table>'
;
    ssi_logout($_SESSION['logout_url']);
}

mysql_free_result($request);
}

?>


Let me know what you think and if you try it out let me know how it works.

Coming soon!

JayBachatero

morph thanks the SSI function works great.  now the other script is nnot working i just get a blank page.  ima take a look to see wts goin on tonight or tom.  but thanks.  i wanted 2 ask you how is pagination done in smf the smf way [1][2]...[6][7]?
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

Advertisement: