Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/2012/Archives for December 2012

Archives for December 2012

December 26, 2012

Remove Last Comma In Loop

When performing a database query, we normally use a loop to output the database result. This is a common exercise for programmers when displaying results to the screen. To make the output readable, commas are typically used to separate data. Consider a loop below that prints a list of names. We added a string ‘, ‘ after each name to make it readable.

foreach($query as $item) :
echo $item->name.', '; 
endforeach;

foreach($query as $item) : echo $item->name.', '; endforeach;

The output would be something like this:
John, Mary, Steve, Mark,

The output works as intended, but there is a trailing comma at the end that we don’t want. We either need to get rid of it, or replace it with a period. So, how do we do that? Here’s how.

We will use the trim() function to remove the last comma. But before we can do that, we should probably hold off on echoing to the screen until the loop is finished. So, we will store the results in a variable called $result as we go through the loop. To avoid a PHP undefined variable error, we need to initialize the $result variable before the loop begins. After the loop, we will use the rtrim function to remove the last comma. Finally, we echo the trimmed results and add a period.

$result="";
foreach($query as $item) :
$result.=$item->name.', '; 
endforeach;
$trimmed=rtrim($result, ', ');
echo $trimmed;
echo '.';

$result=""; foreach($query as $item) : $result.=$item->name.', '; endforeach; $trimmed=rtrim($result, ', '); echo $trimmed; echo '.';

The output is now:
John, Mary, Steve, Mark.

Filed Under: PHP Tagged With: commas, loop

December 19, 2012

Bootstrap Your Website

Jumpstart your website by adding Bootstrap, a front-end CSS framework started by a couple of Twitter developers. If you are starting on a new web project, why not dress it with Bootstrap. You can quickly transform your website, by just adding a couple of markups. Bootstrap uses some HTML markup, LESS CSS, and Javascript plugins to create the framework.

To get started, Download Bootstrap first.

Then, use this HTML Bootstrap template

<!DOCTYPE html>
<html>
  <head>
    <title>Bootstrap 101 Template</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
  <div class="container-fluid">
    <h1>Hello, world!</h1>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </div>
  </body>
</html>

<!DOCTYPE html> <html> <head> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"> </head> <body> <div class="container-fluid"> <h1>Hello, world!</h1> <script src="http://code.jquery.com/jquery-latest.js"></script> <script src="js/bootstrap.min.js"></script> </div> </body> </html>

To learn more about Bootstrap, just play around with all the different aspects of the framework. Add some navigation buttons, forms, tables, alerts, and some Javascript features. Finally, you can learn a lot more by studying the rest of the documentation.

Filed Under: CSS, HTML, JS Tagged With: bootstrap, twitter

December 8, 2012

Dropbox Folder As Web Root

One of many clever ways you can do with Dropbox is to make it the web root of your development server. There’s an advantage to doing this, as I’ll explain in this post. Currently, I have Dropbox installed on my Linux Mint desktop. I also use the desktop as my web development server. I have Apache, PHP, MySQL, etc. installed. Typically the web root of the Apache web server, is located in the /var/www directory. I’ve changed mine to a Dropbox folder at /home/ulysses/Dropbox/web/.

There’s an advantage to making Dropbox the web root. I normally use multiple computers to code. I have a Linux desktop, Macbook Air, and a Windows machine. I installed Dropbox on all of them, and placed my code in a Dropbox folder to share files. My code is always synched on all computers. Each time I move to another computer, I can always access the same code, because Dropbox does a fantastic job of synching files on all my computers.

In addition, I made Dropbox the web root of my development server, so every time I make a file change, the development web server is also updated. So, there is no more need for me to FTP or copy files to my development server. One more thing, if my desktop is turned on at home, any changes I make away from home, are reflected on my development server instantaneously.

To change Apache’s web root, edit the /etc/apache2/sites-available/default config file. Change the default directory from /var/www to /home/ulysses/Dropbox/web/, the location of my Dropbox folder.

DocumentRoot /home/ulysses/Dropbox/web
<Directory />
 Options FollowSymLinks
 AllowOverride None
</Directory>
<Directory /home/ulysses/Dropbox/web/>
 Options Indexes FollowSymLinks Multiviews
 AllowOverride All
 Order allow,deny
 allow from all
</Directory>

DocumentRoot /home/ulysses/Dropbox/web <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /home/ulysses/Dropbox/web/> Options Indexes FollowSymLinks Multiviews AllowOverride All Order allow,deny allow from all </Directory>

Do the same for the /etc/apache2/sites-enabled/default config file.

Finally, restart Apache.

$ sudo /etc/init.d/apache2 restart

$ sudo /etc/init.d/apache2 restart

Filed Under: CSS, HTML, PHP Tagged With: apache, dropbox, web server

  • 1
  • 2
  • Next Page »
  • Cloud
  • Linux
  • Git

Copyright © 2012–2021