Tuesday, September 20, 2016

PHP - Passing Variables with Includes

Anyone into/learning PHP might find this helpful...

Let's say that we have many php pages that share the same `navigation.inc.php` as an "include" file.

Even though the navigation include file is shared we want it to be dynamic enough to highlight the menu bar based on the page that is accessing the include file.

In this example we will only deal with one page - the `About` page, but the same paradigm is followed for all pages.

In the `about.php` page we simply set the `active` variable before the navigation page is included...

    <?php $active = "about"; ?>
    <?php include("navigation.inc.php"); ?>

Then in the navigation page we construct an if condition based on the `active` variable...

    <?php
        if ($active == "about") {
            echo '<li class="current active dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
        } else {
            echo '<li class="dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
        }
    ?>
    <a class=" dropdown-toggle" href="about-us.php" data-target="#" data-toggle="dropdown">About Us <b class="caret"></b></a>

As you can see, the only difference between the two "li" elements in the if condition is that one uses the class "current active" while the other does not. So when the `about` page includes the `navigation` page, the `about` menu item
will be highlighted as the currently active page.

There are a variety of ways to do this, and i am by no means suggesting that this is the best way. I did, however, find it a helpful and interesting way.

Here's another method to consider for accomplishing the same task...

In `about.php` set an "about" variable to the text that you will need in the navigation page to make the `about` menu item highlighted as active...

    <?php $about = "current active"; ?>
    <?php include("navigation.inc.php"); ?>

Then in the navigation file simply call the variable $about, which will have the needed text if the about page is the one including the navigation file OR it  will be blank when another page is the one including the navigation file...

    <?php
        echo '<li class="' . $about . ' dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
    ?>
    <a class=" dropdown-toggle" href="about-us.php" data-target="#" data-toggle="dropdown">About Us <b class="caret"></b></a>

Then for each of the other pages you would simply change the name of the variable to the page name, and reference that variable in the appropriate section of the navigation page...

`home` page...

    <?php $home = "current active"; ?>
    <?php include("navigation.inc.php"); ?>

`navigation` page (home link section)...

    <?php
        echo '<li class="' . $home . '" >';
    ?>
    <a href="index.php" data-target="#">Home</a>

Wednesday, December 23, 2015

HTML with ASP if condition

Here's a little peice of code that can come in handy. It's a way of building HTML code around an ASP if condition. The if condition in this example references the current file name.

<%
Dim my_array, fullname, fname
fullname = Request.ServerVariables("SCRIPT_NAME")
my_array=split(fullname,"/")
fname=my_array(ubound(my_array))
if (fname = "about.asp") then
%>

<ul class="blocklist">
  <li><a href="http://prbseminary.org">Home</a></li>
  <li><a href="http://prbseminary.org/about.asp">About</a></li>
  <li><a href="mailto:admin@prbseminary.org?subject=PRBS Contact">Contact</a></li>
  <li><a href="http://prbseminary.org/donate.asp">Donate</a></li>
  <li><a href="http://prbseminary.org/library.asp">PRBS Library</a></li>
  <li><a href="http://prbseminary.org/academics.asp">Academics</a></li>
  <li><a href="http://prbseminary.org/app/apply.html" target="_blank">Apply Online</a></li>
  <li><a href="http://prbseminary.org/mentor.asp">Mentors</a></li>
  <li><a href="http://prbseminary.org/faq.asp">FAQ</a></li>
</ul>

<% end if %>

Monday, November 30, 2015

Case Insensitive String Compare in C++

Sometimes you want to compare or read a string in 'case-sensitive' mode (i.e. whether a letter is capital or lowercase matters), while at other times you want to compare or read a string without worrying about the case  (i.e. case insensitive). Passwords, for instance, would be best compared/read in case-sensitive mode...while some AI interfaces that want to read what the user is inputing would be best read in case-insensitive mode.

So on to the code. This is a simple way of converting text input to all upper-case, thereby being able to read/compare the input without having to worry about case sensitivity...

#include <iostream>
using namespace std;

void tocaps(char *);
int main()
{
  char input_string[200];
  cout << "Enter a string :"; cin >> input_string;
  tocaps(input_string);
  cout << "After upper case conversion :" <<endl << input_string;
  return 0;
}

void tocaps(char *a)
{
  int i=0;
  while(*(a+i)!='\0')
  {
    if(*(a+i)>=97 && *(a+i)<=122) (*(a+i))-=32;
    i++;
  }
}

Wednesday, November 25, 2015

Old School C String Compare

Even though i prefer C++ to C, sometimes it's good to know a little old school. Here's an example of how one can compare strings with C language. Since the C language does not have a string type, strings are built with arrays or pointers.

#include <stdio.h>
int main() {
  char * first_name = "John"; /* using pointer */
  char last_name[] = "Doe"; /* using local array */
  char name[100];
  /* comparing strings */
  if (strncmp(first_name, "John") != 0) return 1;
  if (strncmp(last_name, "Doe") != 0) return 1;
  sprintf(name, "%s %s", first_name, last_name);
  return 0;
}
To break it down a bit...
The first_name variable is set as a pointer, which means that we can view it but not alter it. The last_name variable is set as a local array, which means we can also alter it. We can alter the last_name in whole or in part since it is constructed as an array.

last_name[] = "Smith"; /* this would change the whole last name to `Smith` */
last_name[0] = "R"; /* this would change the first character in the last name, so that the last name becomes `Roe`  */
You may be asking yourself, why is the array bracket empty? Well,  it doesn't have to be. When it's left empty we allow the system to determine the number of characters. The thing to remember is that the number of characters will be one more than the string that you are defining. This is because in C you need to leave  room for string termination. Therefore...

char last_name[] = "Doe";
/* would be the same as */
char name[4] = "Doe";

The `strncmp` that we used to compare strings earlier could have taken on an additional  argument  - how many characters to compare. So that if we wanted to compare only the first three charaters of the first_name variable we could have done...

  if (strncmp(first_name, "Joh", 3) != 0) return 1;


Thursday, November 19, 2015

Simple Dropdown Menu Button with Bootstrap

Bootstrap is a pretty cool framework that can be used in standard html web pages for some pretty impressive design options.
It's an open source git-hub project: Git-Hub Bootstrap

Here's an example of how easily it is to create a dropdown menu button with the bootstrap framework in html:




<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

</head>

<div id="sidebar" class="column-right">
<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
    <li><a href="#">HTML</a></li>
    <li><a href="#">CSS</a></li>
    <li><a href="#">JavaScript</a></li>
  </ul>
</div>
</div>

Tuesday, November 17, 2015

C++ Simple Example of Using Arguments

C++ is a great language, largely because of its mid-level functionality. It can accomplish things at the system level as well as the user level. Here's  a simple example of working with arguments, including a help message if no arguments are passed...

#include <iostream>
int main(int argc, char* argv[])
  {
    if (argc < 2) {
      std::cerr << "Usage: " << argv[0] << " NAME" << std::endl;
      return 1;
      }
    std::cout << argv[0] << "says hello, " << argv[1] << "!" << std::endl;
    return 0;
  }

A few things to note about the code above:
  • The number of arguments and the argument string is passed directly 'main'.
  • Even though the user is to pass one argument, we check to make sure there are two arguments passed. This is because the program itself is considered the first argument.
  • We return a '1' when an argument is not passed by the user, but only after displaying help to the user. This is to record that the program did not end properly.
  • We return a '0' to show the program was executed properly.


Friday, November 13, 2015

Arrays: Pushes and Pops

Arrays can be a very helpful way of dealing with groups of data. There are a variety of ways of working with arrays through different programming languages, some more complex than others, some more functional, etc.

I love how PHP works with arrays because it makes them easy to create and has some great functions for working with them. Pushes and pops are nice quick ways of adding or taking out members at the end of an array that many programming languages have. I'll be using PHP for my examples.

Push example:
$num_set = [1,2,3,4]; //creates  array $num_set
array_push($num_set, 5); // now array is [1,2,3,4,5]
Notice that one of the unique things with PHP is that you don't have to declare your variables. PHP does the work in identifying what kind of variable it is by seeing what  kind of data is stored in it. I find this quite convenient, though it has its drawbacks as well.
Pop example:
$num_set = [1,2,3,4];
array_pop($num_set); //$num_set is now [1,2,3]
But what if you wanted to deal with more than just the last member of an array? No problemo...that's where array_shift (similar to pop) and array_unshift (similar to push) come in...

array_shift example:
$num_set = [1,2,3,4];
array_shift($num_set); // now array is [2,3,4];
Note: This affects array keys and literal keys differently. The array keys shift with the array change while literal keys to not change.

array_unshift example:
$num_set = [1,2,3,4];
array_unshift($num_set, 0); // "0" is the index of the  array (counting starts with 0 in many programming  languages). Now array is [0,1,2,3,4];
Sometimes it's helpful to merge two arrays together. Depending on the outcome you are looking for you may want to only do a merge, or you may want  to also do a sort.

merge only example:
$random_1 = [38193, 83493, 11027, 34272];
$random_2 = [23115, 12372, 25168, 18307];
$random_all = array_merge($random_1, $random_2);
//result will be a combination of the two  arrays with random_2  following random_1 in the order that they are represented
merge with sort example:
$books_1 = ["The Idiot", "Crime and Punishment", "War and Peace"];
$books_2 = ["Gulliver's Travels", "A Tale of Two Cities", "Pilgrim's Progress"];
$books_all = array_merge($books_1, $books_2);

//at this point the books would be ordered in the position they appear from the first array followed  by the second array.

sort($books_all);

//now the books are listed alphabetically.
But what if you wanted "The Idiot" and "A Tale of Two Cities" to be sorted based on the second word  rather than the first? Well, an easy way to accomplish this  is to set your array up for such by putting the title in like: "Idiot, The" and "Tale of Two Cities, A"