I have just finished another page of my project and everything is working just great with the following code example.
My first concern is whether you could recommend something to make my code look more beautiful or probably more secure? As I have no user input, I suppose this code can not be SQL injected? For connecting to MySQL I use latest ADOdb. I also use pagination class as you will see below (but I didn't attach it here but it's already OO based).
My second question is if I must switch, this style of codes to OO style. I don't really understand why should I use OO so far, because, I have separate file for functions, I never repeat my self (stay "DRY"), as a result, by editing on functions it will change behavior of hundreds of other files? If you have reasons for me to use OO could you please emphasize them and could you please provide the converted working version of my code in OO so I colud study on it and compare it with my code as it's something I wrote and know thoroughly?
Thank you very much in advance,
Ilia
require( 'config.php' );
require( 'function.php' );
$active = ( $config[ 'approve' ] == '1' ) ? " AND v.active = '1'" : NULL;
$page = ( isset( $_REQUEST[ 'page' ] ) && is_numeric( $_REQUEST[ 'page' ] ) ) ? $_REQUEST[ 'page' ] : 1;
$sort = ( isset( $_REQUEST[ 'sort' ] ) && $_REQUEST[ 'sort' ] ) ? $_REQUEST[ 'sort' ] : NULL;
$paginate = ( isset( $_REQUEST[ 'paginate' ] ) && is_numeric( $_REQUEST[ 'paginate' ] ) ) ? $_REQUEST[ 'paginate' ] : NULL;
switch ( $sort )
{
case 'popular':
$orderBy = " ORDER BY v.viewed DESC";
break;
case 'highest':
$orderBy = " ORDER BY (like_count) DESC";
break;
case 'lowest':
$orderBy = " ORDER BY (dislike_count) DESC";
break;
case 'discussed':
$orderBy = " ORDER BY (comment_count) DESC";
break;
case 'featured':
$orderBy = " AND v.featured = 'yes' ORDER BY v.add_time DESC";
$sqlPaginationAppend = " AND featured = 'yes'";
break;
default:
$orderBy = " ORDER BY v.add_time DESC";
break;
}
switch ( $paginate )
{
case '12':
$paginationValue = '12';
break;
case '18':
$paginationValue = '18';
break;
case '24':
$paginationValue = '24';
break;
case '36':
$paginationValue = '36';
break;
case '48':
$paginationValue = '48';
break;
case '60':
$paginationValue = '60';
break;
case '96':
$paginationValue = '96';
break;
default:
$paginationValue = '12';
break;
}
$sqlPaginationAppend = ( isset( $sqlPaginationAppend ) ) ? $sqlPaginationAppend : NULL;
$sqlPaginationAppend .= " AND active = '1'";
$sql = "SELECT count(VID) AS total_videos FROM video WHERE type = 'public'" . $sqlPaginationAppend;
$ars = $conn->execute( $sql );
$total = $ars->fields[ 'total_videos' ];
$pagination = new Pagination( $paginationValue );
$limit = $pagination->getLimit( $total );
$sql =
"SELECT
v.*,
u.username,
count(DISTINCT c.comment_id) AS comment_count,
count(DISTINCT l.UID) AS like_count,
count(DISTINCT d.UID) AS dislike_count
FROM
video AS v
LEFT JOIN
users AS u ON v.UID = u.UID
LEFT JOIN
comments AS c ON v.VID = c.VID
LEFT JOIN
video_rate_likes AS l ON v.VID = l.VID
LEFT JOIN
video_rate_dislikes AS d ON v.VID = d.VID
WHERE
v.type = 'public' " . $active . "
GROUP BY
v.VID
" . $orderBy . "
LIMIT "
. $limit;
$rs = $conn->execute( $sql );
$videos = $rs->getrows();
$paginate ? $paginate .= '/' : NULL;
$pagination_url = $config['BASE_URL']. '/videos/' .$paginate. $sort. '-{#PAGE#}';
$page_link = $pagination->getPagination( $pagination_url );
$start_num = $pagination->getStartItem();
$end_num = $pagination->getEndItem();
$pagination_number = $pagination->getTotalPages();