Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Break Apart Text 2

Status
Not open for further replies.

dagger2002

Programmer
Nov 23, 2004
172
US
ok i have a question.

How do i break apart text.

I have a 4 digit code (YYMM) and want to be able to display it as

0601 = January 2006
0602 = February 2006

and so on.

What do i use to do this?

Thanks alot in advanced
 
I would first have my script create an array where element 01 is "January", element 02 is "February", etc. (i.e. $a = array ('01' => "January, '02' = 'February'.....);)

I would then use substr() to split the string containing the code.

I would take the year-part and add 2000 to it.

I would use the month-part as the index into the month-names array to retrieve the appropriate month-name.







Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir's method is the most robust, i think. as an alternative ... if you can guarantee that the date will be in this century then the following would also work for you.

If you cannot guarantee this then you could build in some logic to protect against errors and still make use of this

Code:
echo date("F Y", strtotime("20".$fourDigitCode."01"));
 
ok im new to arrays, this being my first, I use mysql db array function but have never played with them.

did i do this right?

its not showing anything up on the bar.

Code:
<?php

//	Sorts Data to drop or print
	
//	Split the issue id
	$year = substr($rowRt[newsIss], 0 , 2);
	$month = substr($rowRt[newsIss], 2 , 2);

$year2 = $year + 2000;

//echo $year2;

echo $month;

$month2 = array(
					01 => 'January', 
					02 => 'February', 
					03 => 'March', 
					04 => 'April', 
					05 => 'May', 
					06 => 'June', 
					07 => 'July', 
					08 => 'August', 
					09 => 'September', 
					10 => 'October', 
					11 => 'November', 
					12 => 'December'
				);

echo $month2[$month];

?>
 
and I know the array is working because if i replace this:

echo $month2[$month];

with this:

echo $month2[01];

I get this:

January
 
This version of your code works for me:

Code:
<?php
//    Sorts Data to drop or print
//    Split the issue id

$foo = '0603';

$year = substr($foo, 0 , 2);
$month = substr($foo, 2 , 2);

$year2 = $year + 2000;

$month2 = array(
                    '01' => 'January', 
                    '02' => 'February', 
                    '03' => 'March', 
                    '04' => 'April', 
                    '05' => 'May', 
                    '06' => 'June', 
                    '07' => 'July', 
                    '08' => 'August', 
                    '09' => 'September', 
                    '10' => 'October', 
                    '11' => 'November', 
                    '12' => 'December'
                );

echo $year2 . '   ' . $month2[$month];

?>

Note the tricky part to which I hinted in my original post. The month values your script will pull from the code are strings, so you must have strings as the indeces of the month-name array, not integers.



Want the best answers? Ask the best questions! TANSTAAFL!
 
ok well then how can i make it to where i can pull out the value in $rowRt[newsIss].

In the DB it is setup like so:

Field Type Attributes Null Default
newsIss int(4) UNSIGNED ZEROFILL No 0000
 
Don't thank me, thank the PHP documentation team. They have produced one of the best sets of documentation available.

The PHP online manual is a worthy, well-organized resource that you should make yourself familiar with.



Want the best answers? Ask the best questions! TANSTAAFL!
 
ok guys i have run into a problem and i have been working on it all night.

I have it printing the correct way but thats all it is doing right. It prints every single record and i only want it to print records 1 time.

Current:
REC 1
REC 1
REC 1
REC 2
REC 3
how i would Like
REC 1
REC 2
REC 3

Here is my code i currently have:

Code:
<?php

$queryRt = "SELECT DISTINCT * FROM `stories` WHERE `sto_artType` = '3' AND `newsIss` != " . $rowMain[newsIss] . " AND `sto_include` = '1' LIMIT 0 , 30";
//$queryRt = "SELECT * UNIQ FROM `stories` WHERE `newsIss` != '" . $row1[newsIss] . " AND `sto_include` = 1  AND `stor_artTyp` = '3' ORDER BY sto_title ASC'";
$resultRt = mysql_query($queryRt) or die(mysql_ERROR());//*/("Danger Danger");

//$rowRt = mysql_fetch_array($resultRt);
$rowRt = mysql_fetch_assoc($resultRt);
//	echo $queryRt;

?>
<?php
//    Sorts Data to drop or print
//    Split the issue id

//$foo = '0603';

$foo = $rowRt[newsIss];

$year = substr($foo, 0 , 2);
$month = substr($foo, 2 , 2);

$year2 = $year + 2000;

$month2 = array(
                    '01' => 'January',
                    '02' => 'February',
                    '03' => 'March',
                    '04' => 'April',
                    '05' => 'May',
                    '06' => 'June',
                    '07' => 'July',
                    '08' => 'August',
                    '09' => 'September',
                    '10' => 'October',
                    '11' => 'November',
                    '12' => 'December'
                );

	echo '<a href=newsletterIssue.php?iss=' . $row[newsIss] . '>' . $year2 . '   ' . $month2[$month] . '</a><br />';

while($rowRt = mysql_fetch_assoc($resultRt)){
	$foo = $rowRt[newsIss];

	$year = substr($foo, 0 , 2);
	$month = substr($foo, 2 , 2);
	
	$year2 = $year + 2000;
	
	$month2 = array(
						'01' => 'January',
						'02' => 'February',
						'03' => 'March',
						'04' => 'April',
						'05' => 'May',
						'06' => 'June',
						'07' => 'July',
						'08' => 'August',
						'09' => 'September',
						'10' => 'October',
						'11' => 'November',
						'12' => 'December'
					);
	
	echo '<a href=newsletterIssue.php?iss=' . $rowRt[newsIss] . '>' . $year2 . '   ' . $month2[$month] . '</a><br />';
}

?>
 
Without having access to your database, it's almost impossible for any of us to debug your code.

But as a first step, I would to a pruning and housecleaning. You had code in there repeated for no good reason:

Code:
<?php
$queryRt = "SELECT DISTINCT * FROM stories WHERE sto_artType = '3' AND newsIss != " . $rowMain['newsIss'] . " AND sto_include = '1' LIMIT 0 , 30";
$resultRt = mysql_query($queryRt) or die(mysql_error());

$month2 = array(
	'01' => 'January',
	'02' => 'February',
	'03' => 'March',
	'04' => 'April',
	'05' => 'May',
	'06' => 'June',
	'07' => 'July',
	'08' => 'August',
	'09' => 'September',
	'10' => 'October',
	'11' => 'November',
	'12' => 'December'
);

while($rowRt = mysql_fetch_assoc($resultRt))
{
    $year  = substr($rowRt['newsIss'], 0 , 2);
    $month = substr($rowRt['newsIss'], 2 , 2);
    
    $year2 = $year + 2000;

    echo '<a href=newsletterIssue.php?iss=' . $rowRt['newsIss'] . '>' . $year2 . '   ' . $month2[$month] . '</a><br />';
}

?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
And do yourself a favor...

Don't just take code posted here and typographically graft it into your code. Take what is posted here, learn from it, and write a version more appropriate for your code.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I had it repeated because my server for dome dumb reason skips the first record b4 the while statement.
 
Here is the db setup right out of phpMyAdmin. Hope this helps out a little bit.

Code:
Field   		Type  				Attributes  	Null  	Default  	Extra  

sto_id  		int(4) 				UNSIGNED 		No  	  			auto_increment  	
sto_title  		text 	  						No  	  	  							
sto_text  		text 	  						No  	  	  							
sto_date  		date 	  						No  	0000-00-00  	  				
sto_artType  	enum('1', '2', '3')		  		No  	1  	  							
newsIss  		varchar(4) 	  					No  	0000  	  						
sto_img_source  text 	  						No  	  	  							
sto_img_ht  	int(3) 	  						No  	0  	  							
sto_img_wi  	int(3) 	  						No  	0  	  							
sto_img_alt  	text 	  						No  	  	  							
sto_img_capt  	text 	  						No  	  	  							
sto_include  	enum('0', '1') 	  				No  	1
 
No, that helps not at all. I could deduce the relevant parts of that from your question and code.

Does my version of your code work any better? What does your connection code look like?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Here is my db connect minus username, pass, and dbname

Code:
<?php
				
	//	Connects to Database
		$dbhost = "localhost";
		$dbuser = 'username';
		$dbpass = 'Pass';
		$dbname = "database";
	
		global $dbhost, $dbuser, $dbpass, $dbname;
	
	// Attempt to connect to database server
	
		$link = @mysql_connect($dbhost, $dbuser, $dbpass);
	
	// Attempt to connect to select our database
	
		@mysql_select_db($dbname, $link);
				
?>
 
Why are you doing:

global $dbhost, $dbuser, $dbpass, $dbname;

?

Does all this code you're posting take place in a function or class method?



Want the best answers? Ask the best questions! TANSTAAFL!
 
just straight code. at the top of the page. Because it is used all throughout the page.

The connection string was givin to me so im not sure y they put the global statement in there.

Im still pretty new to php. I can get around in simple stuff and can modify all kinds of code.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top