3 simple questions, plus 1 more

He has: 688 posts

Joined: Feb 2001

I've got most of my php/mySQL scripting done (with my minimal knowledge). If you want to see how it's coming together check out pstvalumni.com/directory Smiling

But I've still got a handful of relatively small issues. I hope this doesn't get confusing but I'm going to try to ask each of my various simple questions in this one post. If any of these questions call for it I'll spawn it off onto a separate thread. Also, some of these questions may be purely php and not mySQL in nature.

-------------------------------------------------

QUESTION #1:

Out of curiosity, what is the significance of \n before the "; at the end of a a print statement? It prints either way, so what does the \n do?

QUESTION #2:

I use a datestamp(Cool field which produces dates like this "20021031". Is there an easy way to make this print/display in any human-fiendly format? Either "10/31/2002" (US format) or "October 31, 2002"?

QUESTION #3:

Related to #2, is there an easy way to make a datestamp(Cool value of 00000000 print as the text "unknown" or to not print anything at all? I suppose this could be for any field value result. Is it possible to do something like 'if fieldname=00000000 then make other-value' (where it would print that other-value instead)?

QUESTION #4 (not as simple as the others):

From my link above, my "Locations" script uses the following method for the query to include multiple non-intiger values (the key being "IN"):

<?php
$query
= \"SELECT * FROM alumni WHERE state IN (\"$location\")\";
?>

My "Last Name Starting With The Letter x" script uses the following method for the query (the keys being "LIKE") and the string variable is formatted as "x%":

<?php
$query
= \"SELECT * FROM alumni WHERE lastname LIKE '$lastname'\";
?>

What I want to do is kind of combine these concepts to display last names beginning with the letters A%,B%,C%,D%,E% (beginning with any of these multiple values). My first attempt failed and I just wanted to know if this can possibly work if I just fiddle around enough with the syntax and formatting? Am I on the right track or is this just all wrong and there's a different way to do this?

He has: 296 posts

Joined: May 2002

Quote: Originally posted by fifeclub
QUESTION #1:

Out of curiosity, what is the significance of \n before the "; at the end of a a print statement? It prints either way, so what does the \n do?

\n denotes a new line. It's like but it only changes how it's displayed in the source. I recommend using it so your source isn't cluttered when people want to view it.

Quote:
QUESTION #2:

I use a datestamp(Cool field which produces dates like this "20021031". Is there an easy way to make this print/display in any human-fiendly format? Either "10/31/2002" (US format) or "October 31, 2002"?

You can use SUBSTR($date, 0, 4) for the year (2002), SUBSTR($date, 4,2) for the month, and SUBSTR($date, 6, Cool for day. In PHP it would look like this:

<?php
$date
= 20021031;
$year = substr($date, 0, 4);
$month = substr($date, 4, 2);
$day = substr($date, 6, 8);

// 10/31/02
$date = $month.\"/\".$day.\"/\".$year;
echo
$date;

// October 31, 2002
$months = array(\"January\", \"Feburary\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\");
$month = $month - 1;
$dmon = $months[$month];

echo (\"
$dmon $day, $year\");
?>

More on Substr

Quote:
QUESTION #3:

Related to #2, is there an easy way to make a datestamp(Cool value of 00000000 print as the text "unknown" or to not print anything at all? I suppose this could be for any field value result. Is it possible to do something like 'if fieldname=00000000 then make other-value' (where it would print that other-value instead)?

<?php
if ($date == '00000000') {
  echo (\
"Unknown\");
} else {
  // ... Date code from above
}
?>

Quote:
QUESTION #4 (not as simple as the others):

From my link above, my "Locations" script uses the following method for the query to include multiple non-intiger values (the key being "IN"):

<?php
$query
= \"SELECT * FROM alumni WHERE state IN (\"$location\")\";
?>

My "Last Name Starting With The Letter x" script uses the following method for the query (the keys being "LIKE") and the string variable is formatted as "x%":

<?php
$query
= \"SELECT * FROM alumni WHERE lastname LIKE '$lastname'\";
?>

What I want to do is kind of combine these concepts to display last names beginning with the letters A%,B%,C%,D%,E% (beginning with any of these multiple values). My first attempt failed and I just wanted to know if this can possibly work if I just fiddle around enough with the syntax and formatting? Am I on the right track or is this just all wrong and there's a different way to do this? [/B]

Look into arrays and the for function some, I asked a similar question. If you want to see the code I got just ask and I'll give you a link to it.

Hope I helped you some, good luck!

[James Logsdon]

He has: 688 posts

Joined: Feb 2001

I got the answer to #4 Laughing out loud

Instead of using "LIKE", the query should be

<?php
$query
= \"SELECT * FROM alumni WHERE lastname RLIKE '^[$lastname]'\";
$result = mysql_query ($query);
?>

And with switching from LIKE to RLIKE, the variable $lastname changes from A% (for just beginning with A) to either ABCD or A-D! Note: the variable is actually [A-D] with brackets but I chose to keep the brackets out of the url part of it.

I love it when things actually work out! Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I prefer to through as much work at the mysql DB as (resonably) possible.

Question 2/3:
datestamp? That's not a mySQL data type. I'm guessing timestamp(Cool. Anyway...

SELECT if(date_field=0, 'unknown', DATE_FORMAT(date_field, '%m/%d/%Y')) as date_field;

/* OR */

SELECT if(date_field=0, 'unknown', DATE_FORMAT(date_field, '%M %e, %Y')) as date_field;
'

Question 4:
The solution above is specific to mySQL. It's using regex to search the records. The section isn't documented well in the mySQL docs (at least, not that I've found).

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.