Jump to content

Recommended Posts

In my table, I have a 10 digit cellphone number stored.  I want to display it on a page in this format 000-000-0000.  I can not think of a way to get it displayed like this.  Is there a simple way that I can not think of?  Please let me know if you have ideas.  Thank you. 

Thank you, That makes a lot of sense, but I have it pulling from a database and I am a little unsure how I should handle the formatting in this case.  I have inserted the code I am working with.  I can not really think of an easy way to do it in this case. 

 

<?php # Script 8.5 - view_users.php 
# (4th version after Scripts 7.4, 7.6, & 8.2)

// This script retrieves all the records from the brothers table.
// This new version paginates the query results.

$page_title = 'View the Current Users';
include ('includes/header.html');

// Page header.
echo '<h1 id="mainhead">Brothers</h1>';

require_once ('mysql_connect.php'); // Connect to the db.

// Number of records to show per page:
$display = 100;

// Determine how many pages there are. 
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

	// Count the number of records
$query = "SELECT COUNT(*) FROM brothers ORDER BY last_name ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
	$num_pages = ceil ($num_records/$display);
} else {
	$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

//Default column Links.
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";	
$link3 = "{$_SERVER['PHP_SELF']}?sort=sta";
$link4 = "{$_SERVER['PHP_SELF']}?sort=ema";
$link5 = "{$_SERVER['PHP_SELF']}?sort=cpa";	
$link6 = "{$_SERVER['PHP_SELF']}?sort=sna";
$link7 = "{$_SERVER['PHP_SELF']}?sort=bda";
$link8 = "{$_SERVER['PHP_SELF']}?sort=hta";	
$link9 = "{$_SERVER['PHP_SELF']}?sort=ssa";
$link10 = "{$_SERVER['PHP_SELF']}?sort=tsa";
$link11 = "{$_SERVER['PHP_SELF']}?sort=gya";	
$link12 = "{$_SERVER['PHP_SELF']}?sort=psa";
$link13 = "{$_SERVER['PHP_SELF']}?sort=pya";	
$link14 = "{$_SERVER['PHP_SELF']}?sort=pca";

//Determine the sorting order.
if(isset($_GET['sort'])) {

//Use the exising sorting order.
switch ($_GET['sort']) {
	case 'lnd':
		$order_by = 'last_name DESC';
		$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
		break;
	case 'lna':
		$order_by = 'last_name ASC';
		$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
		break;

	case 'fna':
		$order_by = 'first_name ASC';
		$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
		break;	
	case 'fnd':
		$order_by = 'first_name DESC';
		$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
		break;

	case 'sta':
		$order_by = 'status ASC';
		$link3 = "{$_SERVER['PHP_SELF']}?sort=std";
		break;	
	case 'std':
		$order_by = 'status DESC';
		$link3 = "{$_SERVER['PHP_SELF']}?sort=sta";
		break;

	case 'ema':
		$order_by = 'email ASC';
		$link4 = "{$_SERVER['PHP_SELF']}?sort=emd";
		break;	
	case 'emd':
		$order_by = 'cell DESC';
		$link4 = "{$_SERVER['PHP_SELF']}?sort=ema";
		break;

	case 'cpa':
		$order_by = 'cell ASC';
		$link5 = "{$_SERVER['PHP_SELF']}?sort=cpd";
		break;	
	case 'cpd':
		$order_by = 'email DESC';
		$link5 = "{$_SERVER['PHP_SELF']}?sort=cpa";
		break;

	case 'sna':
		$order_by = 'sn ASC';
		$link6 = "{$_SERVER['PHP_SELF']}?sort=snd";
		break;	
	case 'snd':
		$order_by = 'sn DESC';
		$link6 = "{$_SERVER['PHP_SELF']}?sort=sna";
		break;

	case 'bda':
		$order_by = 'birthday ASC';
		$link7 = "{$_SERVER['PHP_SELF']}?sort=bdd";
		break;	
	case 'bdd':
		$order_by = 'birthday DESC';
		$link7 = "{$_SERVER['PHP_SELF']}?sort=bda";
		break;

	case 'hta':
		$order_by = 'home_town ASC';
		$link8 = "{$_SERVER['PHP_SELF']}?sort=htd";
		break;	
	case 'htd':
		$order_by = 'home_towm DESC';
		$link8 = "{$_SERVER['PHP_SELF']}?sort=hta";
		break;

	case 'ssa':
		$order_by = 'state ASC';
		$link9 = "{$_SERVER['PHP_SELF']}?sort=ssd";
		break;	
	case 'ssd':
		$order_by = 'state DESC';
		$link9 = "{$_SERVER['PHP_SELF']}?sort=ssa";
		break;

	case 'tsa':
		$order_by = 'tshirt ASC';
		$link10 = "{$_SERVER['PHP_SELF']}?sort=tsd";
		break;	
	case 'tsd':
		$order_by = 'tshirt DESC';
		$link10 = "{$_SERVER['PHP_SELF']}?sort=tsa";
		break;

	case 'gya':
		$order_by = 'grad_year ASC';
		$link11 = "{$_SERVER['PHP_SELF']}?sort=gyd";
		break;	
	case 'gyd':
		$order_by = 'grad_year DESC';
		$link11 = "{$_SERVER['PHP_SELF']}?sort=gya";
		break;

	case 'psa':
		$order_by = 'pledge_semester ASC';
		$link12 = "{$_SERVER['PHP_SELF']}?sort=psd";
		break;	
	case 'psd':
		$order_by = 'pledge_semester DESC';
		$link12 = "{$_SERVER['PHP_SELF']}?sort=psa";
		break;

	case 'pya':
		$order_by = 'pledge_year ASC';
		$link13 = "{$_SERVER['PHP_SELF']}?sort=pyd";
		break;	
	case 'pyd':
		$order_by = 'pledge_year DESC';
		$link13 = "{$_SERVER['PHP_SELF']}?sort=pya";
		break;

	case 'pca':
		$order_by = 'pledge_class ASC';
		$link14 = "{$_SERVER['PHP_SELF']}?sort=pcd";
		break;	
	case 'pcd':
		$order_by = 'pledge_class DESC';
		$link14 = "{$_SERVER['PHP_SELF']}?sort=pca";
		break;

	default:
		$order_by = 'last_name DESC';
		break;

}

// $sort will be appended to the pagination links
$sort = $_GET['sort'];

} else { 
	$order_by = 'last_name ASC';
	$sort = 'lna';
}

// Make the query.
$query = "SELECT user_id, last_name, first_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, status FROM brothers ORDER BY $order_by LIMIT $start, $display";		
$result = mysql_query ($query); // Run the query.

if  (!$result) {
    echo "The Query:
$query
Produced the error:
".mysql_error();
    exit;
}

// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b><a href="' . $link1 . '">Last Name</a></b></td>
<td align="left"><b><a href="' . $link2 . '">First Name</a></b></td>
<td align="left"><b><a href="' . $link3 . '">Status</a></b></td>
<td align="left"><b><a href="' . $link4 . '">Email</b></td>
<td align="left"><b><a href="' . $link5 . '">Cellphone</b></td>
<td align="left"><b><a href="' . $link6 . '">Screen Name</b></td>
<td align="left"><b><a href="' . $link7 . '">Birthday</b></td>
<td align="left"><b><a href="' . $link8 . '">Hometown</b></td>
<td align="left"><b><a href="' . $link9 . '">State</b></td>
<td align="left"><b><a href="' . $link10 . '">T-Shirt</b></td>
<td align="left"><b><a href="' . $link11 . '">Graduation Year</b></td>
<td align="left"><b><a href="' . $link12 . '">Pledge Semester</b></td>
<td align="left"><b><a href="' . $link13 . '">Pledge Year</b></td>
<td align="left"><b><a href="' . $link14 . '">Pledge Class</b></td>
</tr>
';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['last_name'] . '</td>
	<td align="left">' . $row['first_name'] . '</td>
	<td align="left">' . $row['status'] . '</td>
	<td align="left">' . $row['email'] . '</td>
	<td align="left">' . $row['cell'] . '</td>
	<td align="left">' . $row['sn'] . '</td>
	<td align="left">' . $row['birthday'] . '</td>
	<td align="left">' . $row['home_town'] . '</td>
	<td align="left">' . $row['state'] . '</td>
	<td align="left">' . $row['tshirt'] . '</td>
	<td align="left">' . $row['grad_year'] . '</td>
	<td align="left">' . $row['pledge_semester'] . '</td>
	<td align="left">' . $row['pledge_year'] . '</td>
	<td align="left">' . $row['pledge_class'] . '</td>
</tr>
';
}




echo '</table>';

mysql_free_result ($result); // Free up the resources.	

mysql_close(); // Close the database connection.

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<br /><p>';
// Determine what page the script is on.	
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
	echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
	if ($i != $current_page) {
		echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort . '">' . $i . '</a> ';
	} else {
		echo $i . ' ';
	}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
	echo '<a href="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort . '">Next</a>';
}

echo '</p>';

} // End of links section.

include ('includes/footer.html'); // Include the HTML footer.
?>

if you put it in a function

<?php
function format_cellphone ($cellphone)
{
     $cell = str_replace(' ', '', $cellphone);  // remove any spaces
     return  substr($cell,0,3) . '-' . substr($cell,3,3) . '-' . substr($cell,6) ;
}
?>

 

then

<td align="left">' . $row['cell'] . '</td>

// becomes

<td align="left">' . format_cellphone($row['cell']) . '</td>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.