Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
hicuxunicorniobestbuildpc
The Mouse Is Extension Of Arm



Joined: Aug 13, 2009
Posts: 1122

PostPosted: Fri Nov 22, 2013 5:19 am Reply with quote

Guys, I am trying to convert some modules to use single quotes, commas with backticks. Now I tried this but I am not sure if it is correct. Please let me know.


I have this

Quote:
echo("\n");


Can I convert it like this

Quote:
echo('' . PHP_EOL);



This one

Code:
   echo("<td align=\"center\" bgcolor=\"".$bgcolor2."\"><a href=\"" . $self . "&op=author_list\">" . _AUTHOR_LIST . "</a></td>\n");


Can I convert it like this

Code:
echo('<td align="center" bgcolor="' . $bgcolor2 . '"><a href="' . $self . '&op=author_list">' . _AUTHOR_LIST . '</a></td>' . PHP_EOL);
 
View user's profile Send private message
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Fri Nov 22, 2013 6:49 am Reply with quote

This is how I would do it. The first one would be simply this (I don't understand why this module author used echo()):

Code:


echo PHP_EOL;


And the second one would be better as this:

Code:


echo '<td align="center" bgcolor="', $bgcolor2, '"><a href="', $self, '&op=author_list">', _AUTHOR_LIST, '</a></td>', PHP_EOL;

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
hicuxunicorniobestbuildpc







PostPosted: Fri Nov 22, 2013 7:27 am Reply with quote

Thank you very much Montego. Very Happy Great!!

I started converting Nukeblog which has all this strange code.
[ Only registered users can see links on this board! Get registered or login! ]

Example from index.php

Code:
if ( !defined('MODULE_FILE') ) {

    die ('You can\'t access this file directly...');
}
// ///////////////////////////////////////////////////////////////////////////////////
// Take care of PHPNuke Framework.                                       //
// ///////////////////////////////////////////////////////////////////////////////////
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$self = "modules.php?name=" . $module_name;

// ///////////////////////////////////////////////////////////////////////////////////
// Include core and index function libraries.                              //
// ///////////////////////////////////////////////////////////////////////////////////
include("modules/$module_name/functions/functions_common.php");
include("modules/$module_name/functions/functions_index.php");

// ///////////////////////////////////////////////////////////////////////////////////
// Turn right side blocks on or off through NukeBlog Admin Panel.               //
// ///////////////////////////////////////////////////////////////////////////////////

if (!defined('INDEX_FILE')) define('INDEX_FILE', true); // Set to FALSE to hide right blocks
if (defined('INDEX_FILE') AND INDEX_FILE===true) {
$index = 1;
}
// ///////////////////////////////////////////////////////////////////////////////////
// Block against non-site members.                                       //
// ///////////////////////////////////////////////////////////////////////////////////
if (get_config("right_blocks")=="0" && (!$user)){
    include("header.php");
    opentable();
    center("<span class=\"title\">" . _NB_RESTRICTED . "</span>");
    closetable();
    br();
    opentable();
    center(_NB_MEMREQ);
    closetable();
    include("footer.php");
    include ("includes/counter.php");
    die();
}

// ///////////////////////////////////////////////////////////////////////////////////
// Condition user identification variable into a "better" format.               //
// ///////////////////////////////////////////////////////////////////////////////////
$temp_user = base64_decode($user);
$temp_cookie = explode(":", $temp_user);
$sql = "SELECT user_id, username FROM " . $user_prefix . "_users WHERE username='" . $temp_cookie[1] . "'";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
    $nb_user[user_id] = $row[user_id];
    $nb_user[username] = $row[username];
}

// ///////////////////////////////////////////////////////////////////////////////////
// Auto-set $offset to the number stored in the database.                     //
// ///////////////////////////////////////////////////////////////////////////////////
if (!$offset) {
    $offset = 0;
}

// ///////////////////////////////////////////////////////////////////////////////////
// Include site header file.                                          //
// ///////////////////////////////////////////////////////////////////////////////////
include("header.php");

// ///////////////////////////////////////////////////////////////////////////////////
// Standard blog user menu.                                             //
// ///////////////////////////////////////////////////////////////////////////////////
user_menu();

// ///////////////////////////////////////////////////////////////////////////////////
// Traffic control switch/case for the browsing and interacting with other's blogs.   //
// ///////////////////////////////////////////////////////////////////////////////////
switch ($op) {

    case "fetch_author":
        fetch_author($user_id,$offset);
        break;

    case "blog_members":
        blog_members();
        break;

    case "comment_save":
        comment_save($blog_id,$comm_body);
        break;

    case "comment_add":
        comment_add($blog_id);
        break;

    case "fetch_blog":
        fetch_blog($blog_id);
        break;

    case "blog_alert_save":
        blog_alert_save($form);
        break;

    case "admin_blog_alert":
        admin_blog_alert($blog_id);
        break;

    case "blog_list":
        blog_list($offset);
        break;

    default:
        blog_list($offset);
        break;
}

// ///////////////////////////////////////////////////////////////////////////////////
// PHPNuke Footer.                                                   //
// ///////////////////////////////////////////////////////////////////////////////////
include("footer.php");
include ("includes/counter.php");
die();


I did this. I found this module very strange they way was coded.

New

Code:
if (!defined('MODULE_FILE')) die('You can\'t access this file directly...');


//$index = 0;
if (!defined('INDEX_FILE')) define('INDEX_FILE', true); // Set to FALSE to hide right blocks
if (defined('INDEX_FILE') AND INDEX_FILE === true) {
   // auto set right blocks for pre patch 3.1 compatibility
   $index = 1;
}
require_once 'mainfile.php';
$module_name = basename(dirname(__FILE__));
get_lang($module_name);

$self = 'modules.php?name=' . $module_name;


include_once 'modules/' . $module_name . '/functions/functions_common.php';
include_once 'modules/' . $module_name . '/functions/functions_index.php';

if (get_config('right_blocks')=='0' && (!$user)){
    include_once 'header.php';
    opentable();
    center('<span class="title">' . _NB_RESTRICTED . '</span>');
    closetable();
    br();
    opentable();
    center(_NB_MEMREQ);
    closetable();
    include_once 'footer.php';
    include_once 'includes/counter.php';
    die();
}

$temp_user = base64_decode($user);
$temp_cookie = explode(':', $temp_user);
$sql = 'SELECT `user_id`, `username` FROM `' . $user_prefix . '_users` WHERE `username` = \'' . $temp_cookie[1] . '\'';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
    $nb_user[user_id] = $row[user_id];
    $nb_user[username] = $row[username];
}

if (!$offset) {
    $offset = 0;
}

include_once 'header.php';

user_menu();

switch ($op) {

    case 'fetch_author':
        fetch_author($user_id,$offset);
        break;

    case 'blog_members':
        blog_members();
        break;

    case 'comment_save':
        comment_save($blog_id,$comm_body);
        break;

    case 'comment_add':
        comment_add($blog_id);
        break;

    case 'fetch_blog':
        fetch_blog($blog_id);
        break;

    case 'blog_alert_save':
        blog_alert_save($form);
        break;

    case 'admin_blog_alert':
        admin_blog_alert($blog_id);
        break;

    case 'blog_list':
        blog_list($offset);
        break;

    default:
        blog_list($offset);
        break;
}

include_once 'footer.php';
include_once 'includes/counter.php';
die();


I noticed it's been used echo all the time but I will like to use only one echo and the rest comma,

This is what I did

Code:
echo '<table cellpadding="3" cellspacing="1" border="0" align="center">' , PHP_EOL;

   echo '<tr>' . PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=settings">' , _NB_SETTINGS , '</a></td>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=remove_blog">' , _BLOG_REMOVE , '</a></td>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=remove_comment">' , _COMM_REMOVE , '</a></td>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=author_list">' , _AUTHOR_LIST , '</a></td>' , PHP_EOL;
   echo '</tr>' , PHP_EOL;
   echo '<tr>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=admin_alerts">' , _ADMIN_ALERTS , '</a>' , PHP_EOL;
   if($num_alerts != 0) {
      echo(" (" , $num_alerts , ")");
   }
   echo '</td>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=mood_list">' , _MOOD_MAN , '</a></td>' , PHP_EOL;
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="admin,php">' , _NUKE_ADMIN , '</a></td>' , PHP_EOL);
   echo '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="http://www.trevor.net/modules.php?name=Forums&file=viewforum&f=15" target="_blank">' , _NB_HOME , '</a></td>' , PHP_EOL);
   echo '</tr>' , PHP_EOL);
   echo '</table>' , PHP_EOL);


but I am so happy with the result. I would like to get rid of all echo's

Can I use this?

Code:
   echo '<table cellpadding="3" cellspacing="1" border="0" align="center">' , PHP_EOL

   , '<tr>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=settings">' , _NB_SETTINGS , '</a></td>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=remove_blog">' , _BLOG_REMOVE , '</a></td>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=remove_comment">' , _COMM_REMOVE , '</a></td>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=author_list">' , _AUTHOR_LIST , '</a></td>' , PHP_EOL
   , '</tr>' , PHP_EOL
   , '<tr>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=admin_alerts">' , _ADMIN_ALERTS , '</a>' , PHP_EOL
   if($num_alerts != 0) {
      , '' , $num_alerts , '';//This one I am not sure how to do convert it
   }
   , '</td>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="' , $self , '&op=mood_list">' , _MOOD_MAN , '</a></td>' , PHP_EOL
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="admin,php">' , _NUKE_ADMIN , '</a></td>' , PHP_EOL)
   , '<td align="center" bgcolor="' , $bgcolor2 , '"><a href="http://www.trevor.net/modules.php?name=Forums&file=viewforum&f=15" target="_blank">' , _NB_HOME , '</a></td>' , PHP_EOL)
   , '</tr>' , PHP_EOL)
   , '</table>' , PHP_EOL);


Note: Can u please explain with more details. I am interesting to know this.
 
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Fri Nov 22, 2013 1:12 pm Reply with quote

Yes you can do it that way(see your last code snippet), you don't have to use echo for each seperate line, you can concatenate the whole thing- this is just a personal preference thing really. Calling echo less might be infinitesimally quicker but the trade off (there is always a trade-off somewhere) is I think the code is less readable.

Personally, I would probably do something else as well to shorten the lines because each character consumes resources (untested for you to play with).......
Code:


$tdstyle = 'style="text-align:center; background-colour:'.$bgcolor2.';"';
echo '<table cellpadding="3" cellspacing="1" border="0" align="center">' , PHP_EOL
   , '<tr>' , PHP_EOL
   , '<td '.$tdstyle.'><a href="' , $self , '&op=settings">' , _NB_SETTINGS , '</a></td>' , PHP_EOL
 
 
View user's profile Send private message Send e-mail
hicuxunicorniobestbuildpc







PostPosted: Sun Nov 24, 2013 7:04 am Reply with quote

Wow that has sense Guardian

Do you think I must create a global or it is not necessary?

Code:
global $self, $tdstyle, $bgcolor2, $db, $prefix;


So u mean this which will affect all the lines.

Code:
  $tdstyle = 'style="text-align:center; background-colour:'.$bgcolor2.';"';

   echo '<table cellpadding="3" cellspacing="1" border="0" align="center">' , PHP_EOL
   , '<tr>' , PHP_EOL
  , '<td ' . $tdstyle . '><a href="' , $self , '&op=settings">' , _NB_SETTINGS , '</a></td>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="' , $self , '&op=remove_blog">' , _BLOG_REMOVE , '</a></td>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="' , $self , '&op=remove_comment">' , _COMM_REMOVE , '</a></td>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="' , $self , '&op=author_list">' , _AUTHOR_LIST , '</a></td>' , PHP_EOL
   , '</tr>' , PHP_EOL
   , '<tr>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="' , $self , '&op=admin_alerts">' , _ADMIN_ALERTS , '</a>' , PHP_EOL
   if($num_alerts != 0) {
      , '' , $num_alerts , '' , PHP_EOL//This one I am not sure how to do convert it
   }
   , '</td>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="' , $self , '&op=mood_list">' , _MOOD_MAN , '</a></td>' , PHP_EOL
   , '<td ' . $tdstyle . '><a href="admin,php">' , _NUKE_ADMIN , '</a></td>' , PHP_EOL)
   , '<td ' . $tdstyle . '><a href="http://www.trevor.net/modules.php?name=Forums&file=viewforum&f=15" target="_blank">' , _NB_HOME , '</a></td>' , PHP_EOL
   , '</tr>' , PHP_EOL
   , '</table>' , PHP_EOL;
    closetable();
    echo PHP_EOL;
}
 
Guardian2003







PostPosted: Sun Nov 24, 2013 7:38 am Reply with quote

You don't need a global decleration because you only need it in the scope of when you are printing the table. Yes you can use it throughout the table to style the TD elements (provided they all use the same style) as you have shown in your last code example.
 
hicuxunicorniobestbuildpc







PostPosted: Sun Nov 24, 2013 8:32 am Reply with quote

This is what I did

Code:
function admin_menu()

{
    global $self, $bgcolor2, $db, $prefix;
   $sql = 'SELECT `alert_id` FROM `' . $prefix . '_blog_alerts`';
    debug($sql);
    $result = $db->sql_query($sql);
    $num_alerts = $db->sql_numrows($result);
    opentable();
    $tdstyle = 'style="text-align:center; background-colour:' . $bgcolor2 . ';"';
   echo '<table cellpadding="3" cellspacing="1" border="0" align="center">' , PHP_EOL
   , '<tr>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=settings">' , _NB_SETTINGS , '</a></td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=remove_blog">' , _BLOG_REMOVE , '</a></td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=remove_comment">' , _COMM_REMOVE , '</a></td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=author_list">' , _AUTHOR_LIST , '</a></td>' , PHP_EOL
   , '</tr>' , PHP_EOL
   , '<tr>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=admin_alerts">' , _ADMIN_ALERTS , '</a>' , PHP_EOL
   if($num_alerts != 0) {
      , '' , $num_alerts , '' , PHP_EOL
   }
   , '</td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="' , $self , '&op=mood_list">' , _MOOD_MAN , '</a></td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="admin.php\">' , _NUKE_ADMIN , '</a></td>' , PHP_EOL
   , '<td ' , $tdstyle , '><a href="http://www.trevor.net/modules.php?name=Forums&file=viewforum&f=15" target="_blank">' , _NB_HOME , '</a></td>' , PHP_EOL
   , '</tr>' , PHP_EOL
   , '</table>' , PHP_EOL;
    closetable();
    echo PHP_EOL;
}


I have a question about semicolon ;

I noticed all them ended with semicolon but I start without and when the table close I use semicolon

What is the trick and when I must use it where I can't

Code:
' , PHP_EOL//here ends without

' , PHP_EOL;//here ends with
 
Guardian2003







PostPosted: Sun Nov 24, 2013 2:10 pm Reply with quote

The semi-colon is used at the end of an instruction so PHP knows it has finished that particular job. In the case of your echo statements, you only need one at the end of the echo instruction, not at the end of every line because it is actually one instruction, even though it spans multiple lines on the page.
In your example above, you need to terminate the echo instruction above the line
Code:
if($num_alerts != 0)......
because the IF statement is another instruction.
Likewise after the IF, you will need to start the echo instruction again.
 
hicuxunicorniobestbuildpc







PostPosted: Sun Nov 24, 2013 4:33 pm Reply with quote

Shocked Wow! that is the case but then we should use semicolons after every statement in JavaScript right? Maybe is another thing I will need to learn later but I will make some examples in order to know if I understood well. Thanks for the explanation. Very Happy

So in this case when it is not using echo so we should use at the end semicolon right?

Code:
      $rateinfo .= '   </form>' . PHP_EOL;

      $rateinfo .= '</div>' . PHP_EOL;


These two lines above are instructions too right?
 
Guardian2003







PostPosted: Sun Nov 24, 2013 6:17 pm Reply with quote

Yes those 2 lines are seperate instructions because they are appending data to the $rateinfo variable.

If those two lines existed in real code, you could also do
Code:


 $rateinfo .= ' </form>' . PHP_EOL . '</div>' . PHP_EOL;

This would output exactly the same code to the browser but you have used one line instead of two, which might arguable be faster because PHP wouldn't need to look up the $rateinfo variable and append data to it the second time and it uses less characters and therefore reduces the file size that needs to be read.
HOWEVER, I think using two lines (in your example) makes the code more readable or you could do something else entirely;
Code:


$rateinfo .= '</form>' . PHP_EOL .'
                  . '</div>' . PHP_EOL;

It doesn't matter what code you use, there is always a trade off somewhere and personally I prefer to see readable code rather than shave off a few hundredths of a second in processing speed. Be consistent.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©