Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

This post will explain how to link images, header and footer files across all folders. For example, let’s say you want to create a subfolder for seo purposes. In this case, let’s create a new folder called new-york. Meanwhile, our files in the root folder had all the css, images and javascript. Since the plan is to keep the same look, we only want to use the header and footer file from the root folder and allow those links to work perfectly.

However, if you do this and include the header to the subfolder with ‘include(“../header.php”);’, it will include that file. The issue is that the default path for the the files in the subdirectory will not link up because the path takes on the folder of the file in the new-york directory.

Luckily, there is one easy trick to allow everything to work flawlessly, thus, you only need one file in the new-york folder to utlize all css, images and js of the root folder. The simple line of code is the self closing base tag. An example is shown below with a line of code that can be added to the file in the new-york directory.

Do note that placement is critical. It should be positioned above all links to css and js; otherwise any links above it will use the path in the current directory. Both examples shown below will work.

<base href="example.com" />

 

<base href="/" />

The min() function in a mysql query will get the lowest desired value for a given field. The example below will obtain the oldest date between ‘2012-01-02’ and now.

<?php
include('connect.inc');
$db = public_db_connect();
public_db_connect();

$command = "SELECT min(date) as date FROM table_sort WHERE date < now() AND date > '2012-01-01' ";
$result = mysqli_query($db, $command);

while ($row = mysqli_fetch_assoc($result)) {
    $date = $row['date'];
    echo $date;
}

The two examples will show how to make upper and lowercase strings using PHP and mySQL. With mySQL, it is performed in the query. With PHP, built in PHP functions are used.

<?php
include('connect.inc');
$db = public_db_connect();

$command = "SELECT DISTINCT LOWER(firstname) as first, UPPER(lastname) as last, LOWER(Email) as email FROM table_sort WHERE id >0 ORDER BY last ASC";
$result = mysqli_query($db, $command);
while ($row = mysqli_fetch_assoc($result)) {
    $firstname = $row['first'];
    $lastname = $row['last'];
    $email = $row['email'];
    echo $firstname . "-" . $lastname . "-" . $email . "<br/>";
}
<?php
include('connect.inc');
$db = public_db_connect();

$command = "SELECT DISTINCT firstname, lastname, email FROM table_sort WHERE id >0 ORDER BY lastname ASC" ;
$result = mysqli_query($db, $command);
while($row = mysqli_fetch_assoc($result)){
    $firstname = strtolower($row['firstname']);
    $lastname = strtoupper($row['lastname']);
    $email = $row['email'];
    echo $firstname."-".$lastname."-".$email."<br/>";
}

The demo will shows how to remove ‘cm’ and ‘lbs’ from strings in a table. For example, there are 2 columns in a table; one for weight and the other for height. The data in each table shows numbers like 180 cm and 175 lbs. But, for our records, we want just the numbers like 180 and 175.

The code does a 2 replacements for height and weight. As you can see, whitespace is removed and so are the ‘cm’ and ‘lbs’.

<?php
include('connect.inc');
$db = public_db_connect();

$command = "SELECT DISTINCT LOWER(firstname) as first, LOWER(lastname) as last, LOWER(Email) as email,  replace(replace(height,' ',''),'cm','') as height, replace(replace(weight,' ',''),'lbs','') as weight, date FROM table_sort WHERE id >0 ORDER BY last ASC";
$result = mysqli_query($db, $command);
while ($row = mysqli_fetch_assoc($result)) {
    $firstname = $row['first'];
    $lastname = $row['last'];
    $email = $row['email'];
    $height = $row['height'];
    $weight = $row['weight'];
    echo $firstname . "-" . $lastname . "-" . $email . "-" . $height . "-" . $weight . "<br/>";
}

The trim() is very useful to remove whitespace before and after any data; thus your strings will be precise characters. The code below shows a form. When the form is submitted, the $_POST variables are escaped and trimmed of any potential whitespace.

<?php
include('connect.inc');
$db = public_db_connect();
if (count($_POST) > 0) {

    $firstname = mysqli_real_escape_string($db, trim($_POST['firstname']));
    $lastname = mysqli_real_escape_string($db, trim($_POST['lastname']));
    $height = mysqli_real_escape_string($db, trim($_POST['height']));
    $email = mysqli_real_escape_string($db, trim($_POST['email']));
    $age = mysqli_real_escape_string($db, trim($_POST['age']));
    $weight = mysqli_real_escape_string($db, trim($_POST['weight']));
    $command2 = "INSERT INTO table_sort3 VALUES (NULL, '$lastname', '$firstname', '$email', '$height', '$age', '$weight', now()) ";
    $result2 = mysqli_query($db, $command2);
    if ($result2) {
        echo "Successsul Update!";
    }
}

?>
<form method="post" action="">
    Lastname:<input type="text" name="lastname" value=""/><br/>
    Firstname:<input type="text" name="firstname" value=""/><br/>
    Email:<input type="text" name="email" value=""/><br/>
    Height:<input type="text" name="height" value=""/><br/>
    Age:<input type="text" name="age" value=""/><br/>
    Weight:<input type="text" name="weight" value=""/><br/>
    <input type="submit" name="submit" value="Submit"/>
</form>

The following code will grab rows from a table and add the to the $members array. During each loo, the in_array() function checks and makes sure the value from the ‘id’ column is not a duplicate. The ‘continue;’ will restart the loop if a duplicate exists.

This is just a demo. In reality, the ‘id’ field would be a primary key and autoincrement, thus, the array would be unique anyhow.

<?php
include('connect.inc');
$db = public_db_connect();
$members = array();
$command = "SELECT * FROM table_sort WHERE id > 0";
$result = mysqli_query($db, $command);

while ($row = mysqli_fetch_assoc($result)) {
    $memberID = $row['id'];
    $first_name = $row['firstname'];
    $last_name = $row['lastname'];
//echo $memberID;

    if(in_array($row['id'], $members)){continue;}
    $members[] = $memberID.",".$first_name.",".$last_name;
}

print_r($members);

Here is a simple example that shows how to combine arrays with data queries from two database tables.

<?php
include('connect.inc');
$db=public_db_connect();

// names from table #1
$command = "SELECT * FROM table_sort ";
$result = mysqli_query($db, $command);

while($row = mysqli_fetch_assoc($result)){

    $firstname = $row['firstname'];
    $lastname =  $row['lastname'];
    $array_list[] = $firstname."-".$lastname;
}

// names from table #2
$command2 = "SELECT * FROM table_sort2 ";
$result2 = mysqli_query($db, $command2);

while($row = mysqli_fetch_assoc($result2)){

    $firstname = $row['firstname'];
    $lastname =  $row['lastname'];
    $array_list[] = $firstname."-".$lastname;
}

// names from table #3
$command3 = "SELECT * FROM table_sort3 ";
$result3 = mysqli_query($db, $command3);
while($row = mysqli_fetch_assoc($result3)){

    $firstname = $row['firstname'];
    $lastname =  $row['lastname'];
    $array_list[] = $firstname."-".$lastname;
}

print_r($array_list);

You can use the variable $page_title in the head section to create the page title within the meta tags. The example is shown below.

<?php
//get the url of the page with the curl() function
$my_url = curl();

$findme = 'mysite.com/category/subcategory';
$pos = strpos($my_url, $findme);

if ($pos == true) {

    $my_url_trimmed = str_replace("http://www.mysite.com/category/subcategory/", "", $my_url);

    $command = "SELECT tb1.id, tb1.col1, tb1.col2 from tablename as tb1 WHERE tb1.id='$my_url_trimmed';";
    $result = mysqli_query($db, $command);
    if ($result && mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {

            $space = " ";
            $gap = " | ";
            $location = $row["col1"];
            $page_title = $row["col2"] . $space . $location . $gap;

        }
    }
}

<title><?php echo $page_title; ?> </title>