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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting simple php code 2

Status
Not open for further replies.

kupe

Technical User
Sep 23, 2002
376
The following filched code works to run a list of available stories on a page and when one is clicked, the full story shows.

But there are problems with it.

1. I would like to Date_Format() NewsDate but none of my variations work.

2. I'd like to be able to use an h tag on Headlines for the initial list of stories. I can make Headline bold, but nothing else works.

Be grateful and immensely relieved for guidance.


if($id) {$result = mysql_query("SELECT * FROM studentnews WHERE NewsID=$id", $db);
$myrow = mysql_fetch_array($result);
printf("<h4>%s\n</h4>", $myrow["Headline"]);
printf(" %s\n<br>", $myrow["NewsStory"]);
printf("<div class=\"names\">- %s</div>", $myrow["Byline"]);
printf("Bulletin: %s", $myrow["NewsDate"]);

} else {
//show archives list
$result = mysql_query("SELECT * FROM studentnews ORDER BY NewsDate DESC", $db);
if ($myrow = mysql_fetch_array($result)) {
do {
printf("<a href=\"%s?id=%s\"> <b>%s</b></a><br> %s\n<br> %s\n<p></p>",
$PHP_SELF,$myrow["NewsID"], $myrow["Headline"], $myrow["Blurb"], $myrow["NewsDate"]);

} while ($myrow = mysql_fetch_array($result));
} else {
echo "Sorry, nuttin' found";
}
}
 
1. I don't understand this question (may be my limited english :D )

2. may be your H4 tag is formated with the same size of your normal font. try modifying the style:

[tt]
<style>
h4 { font-face: arial, helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}
</style>
[/tt]

it should be inside the <head> tags.
 
Chacalinc

Thank you. No, it's the other Headline, included in this line ...

printf("<a href=\"%s?id=%s\"> <b>%s</b></a><br> %s\n<br> %s\n<p></p>",

that I cannot get to accept an <h4-or-anything> tag.
Cheers
 
ok, you want this piece of text with more size?

[tt]
printf("<a href=\"%s?id=%s\"> [highlight]<b>%s</b>[/highlight]</a><br> %s\n<br> %s\n<p></p>", [/tt]

ok, then style should be applied to the <a> tag, say:
[tt]
<style>
a.headline { font-face: arial, helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}
</style>

..
printf("<a [highlight]class=\"headline\"[/highlight] href=\"%s?id=%s\"> <b>%s</b></a><br> %s\n<br> %s\n<p></p>", [/tt]
[/tt]
 
Chacalinc

Many thanks ... only how come it goes in there? Shouldn't it go in the

<b>%s</b>
part?

I use CSS by the way to set the h tags et al. Most grateful for your advice. Cheers.
 
you're conditionally looping the code twice. Any reason?

it might be better to use the method below

i'd also consider using \r\n as the new line creator as this appears to work on *nix whereas \n doesn't work alone on windows.

on the date front, call strtotime on the variable first, then call the date function on the resultant timestamp. have implemented a simple d-m-yyyy variant below.

on the formatting - the H4 thing doesn't work because the headline is inside an anchor tag. instead either fix the anchor in css or just add a style attribute such as that done below (turning the headline red). even this may get overwritten by default browser behaviour with links unless you explicitly set the styles for a:hover a:visit a:link and a etc

Code:
$result = mysql_query("SELECT * FROM studentnews ORDER BY NewsDate DESC", $db);
if (mysql_num_rows($result) === 0)
{
  echo "No records returned";
}
else
{

  while ($myrow = mysql_fetch_assoc($result)) 
  {
       //there may be a problem with the syntax of the sql query so try a debug dump here
      echo "<pre>";
      print_r($myrow);
      echo "</pre><br />"; 
      
      printf("<a style=\"color:red;\"href=\"%s?id=%s\"> <b>%s</b></a><br /> %s\n<br /> %s\n<p></p>",  
      $_SERVER['PHP_SELF'],$myrow['NewsID'], $myrow['Headline'], $myrow['Blurb'], date("m-D-Y", strtotime($myrow['NewsDate'])));
            
   } 
}
 
No, the <b> tag is used only to bold the text.

What I did is just use CSS to tha <a> tag.

Cheers.
 
Thanks, jpadie. I will try that.

Where you have "style=\"color:red;" I would like to have <h5> or similar. But where would I put </h5>, please?

All the best
 
Sorry, Chacalinc, we crossed in the uploading.

Where I have <b> is where I would like to have the <h5> </h5>tags. Only I can't get them to work in that position. Cheers
 
do you mean in this line?

printf("<a style=\"color:red;\"href=\"%s?id=%s\"> <b>%s</b></a>

should be:

[tt]printf("<a href=\"%s?id=%s\"> [highlight]<h5 style=\"color:red;\">[/highlight]%s[highlight]</h5>[/highlight]</a>[/tt]

 
Chacalinc

Thanks, that's working there now, thanks very much.

I'm trying it with a variation of jpadie's code. I'm not using the

echo "<pre>";

because I want to use my own layout. It works but not too well.

The basic idea is to offer brief details of the stories available. Then the story you click on appears in full.

This way offers all the stories, twice.

Guess i'll have to go back to the previous code.

Cheers
 
no - the echo lines and intermediate print_r was just debug code in case there was anything wrong in your select statement. delete those and you should get back to a once only story.

 
Cheers, jpadie. Appreciated very much, and it all looks much better.

But one point, with the date() function, how do I ask for a full month, please (ie March rather than Mar)?

(My book has DATE_FORMAT but not DATE()) Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top