help with php logic

scarer - November 23, 2007 - 03:52

Hi,

I'm trying to write some code to display book pages on a page with specialized formatting. I think my logic is a bit awry with the for loop I'm using as it only iterated through this once and catches a non-unique id. The others, it misses altogether. This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Current IT Newsletter | Printable version</title>
<style type="text/css" media="all">@import "misc/print.css";</style>
</head>

<body>
<div align="center">
<div id="banner"></div>
<?php

/*
Author: Sarah Vardy
Date: 20-11-2007
*/

$matchCount = 0;
  

$counter = -1;

//get current month
$tmpMonth = date('m');
$tmpYear = date('Y');

//time formats
$format = '%m-%Y';

//array of month values
$monthArray = array (January,February,March,April,May,June,July,August,September,October,November,December);
print
'<div id="masthead"><img src="newsletter/griffith_masthead.gif"></img></div>';

print
'<div align="left">Newsletter for the month of ' . $monthArray[$tmpMonth -1] . ', ' . $tmpYear . "<br><br></div>";

$nidArray = array();//array to store node ids of docs

//linking to the mysql database
$link = mysql_connect( "localhost", "rcsnews", "");
mysql_select_db("rcsnews2db", $link);


$result1 = mysql_query("SELECT created, body, node.title, node_revisions.nid, filepath FROM node LEFT JOIN node_revisions ON node.nid = node_revisions.nid LEFT JOIN files ON node_revisions.nid = files.fid WHERE type = 'book' ORDER BY created DESC");


while (
$node = mysql_fetch_object($result1)) {

  
$storeTime = ($node->created);
  
$tmpTime  = strftime($format, $storeTime);
  
$tmpBody = $node->body;
  
$tmpTitle = $node->title;
  
$currentTime = strftime($format, time());
  
$tmpFilepath = $node->filepath;
  
//call a function to process the results
  
$counter++;
  
processResults($tmpTime, $tmpBody, $currentTime, $tmpTitle, $tmpFilepath, $counter);

}

/* need to extract the current year and month and compare them to the year and month stored in the array */

function processResults($tmpTime, $tmpBody, $currentTime, $tmpTitle, $tmpFilepath, $counter)
{

  
//need to check if the document has been printed before
  
$tmpTitleArray[$counter] = $tmpTitle;
  
   for(
$j=0; $j< count($tmpTitleArray); $j++)
   {
  
  
//if (true(checkTitle($tmpTitleArray, $tmpTitle)))
   //{
          
if ($tmpTitle == $tmpTitleArray[$j])
           {
             
//print 'Found a match';
             
$matchCount++;
           
//print $tmpTitleArray[$j];
           
return;
     
           }
           else if (
$tmpTitle != $tmpTitleArray[$j])
           {
               if (
$tmpTime == $currentTime && $matchCount < 1)
            {
                   print
'<div align="left"><div id="h1">' . $tmpTitle '</div></div><br><br><div id="postbody">' . '<img src="' . $tmpFilepath . '" />' . "<br>" . $tmpBody . "<br><br></div>";
                return;

            }
            else
            {
                print
'Not this doccy!';
                return
$matchCount;
            }
     
           }
        return;
 
    }
//end for statement
return;

}
//end processResults



mysql_close($link);

?>


</div>
</body>
</html>

Please help me!

Thanks in advance,

Sarah

It would help if you said what you are trying to do

nevets - November 23, 2007 - 04:07

It would help if you said what you are trying to do, also bad practice and no need to call mysql_connect(), mysql_select_db(), and mysql_close(). Also you should use db_query() instead of mysql_query().

what I'm trying to do

scarer - November 26, 2007 - 00:46

I am trying to display all the book articles, the body, the title, the teaser with formatting that I provide. I have a print link for the user to access a printer firendly version of the newsletter for the month. I am just using a straight php page to spit out the results.

I have slightly changed the code and made some variables static. It's still beahving a bit strangely.

The variables are changing but duplicates are still being printed.

It is unclear what processResults() is suppose to do

nevets - November 26, 2007 - 03:18

Because you are query using the files table you can get more than one row per nid (in the case the node has more than one attachment). You seem to be only printing each node once and using the first attachement found. Do you care which attachment is used? It also appears you only want books pages created in the current month, that can be achieved as part of the query (more efficent), is this also correct?

The bottom line what is the goal/purpose of processResults() ?

yes

scarer - November 26, 2007 - 04:46

at this point i don't really mind about the picture but if i can get the original image and just give it a max width that should be fine.

yes i only want the articles for the current month.

processResults() just prints out the stuff that I want ie of the same month and only one instance of each node.

If I understand correctly

nevets - November 26, 2007 - 16:07

If I understand correctly the following PHP code will do what you want. Most of the work has been moved to the query which only returns results for the current month. The "GROUP BY" means you will only get each node once. As written it only returns nodes with an attachment. If you want nodes without an attachment change the last JOIN to a LEFT JOIN. It also fixes your join on files to uses files.nid (instead of files.fid).

<?php
$this_month
= date('m-Y');

$sql = "SELECT created, body, node.title, node_revisions.nid, filepath, FROM node JOIN node_revisions ON node.nid = node_revisions.nid JOIN files ON node_revisions.nid = files.nid  WHERE type = 'book' AND FROM_UNIXTIME(created, '%m-%Y') = '%s' GROUP BY nid";
$results = db_query($sql, $this_month);
while (
$data = db_fetch_object($results) ) {
print
'<div align="left"><div id="h1">' . $data->title '</div></div><br><br><div id="postbody">' . '<img src="' . url($data->filepath) . '" />' . "<br>" . $data->body . "<br><br></div>";
}
?>

doesn't seem to work

scarer - November 27, 2007 - 01:49

Hi Nevets,

I tested the code and it doesn't ever seem to get into the while query.

I'll try it as a block and see if it works.

Thanks for your help on this one :)

Sarah

hi it still doesn't seem to work as a block

scarer - November 27, 2007 - 01:58

Hi,

I tried it as a block and changed the code to this:

<?php

$this_month
= date('m-Y');

$sql = "SELECT created, body, n.title, nr.nid, filepath FROM {node} n JOIN {node_revisions} nr ON n.nid = nr.nid JOIN {files} f ON nr.nid = f.nid  WHERE type = 'book' AND FROM_UNIXTIME(created, '%m-%Y') = '%s' GROUP BY nid";

$results = db_query($sql, $this_month);
while (
$data = db_fetch_object($results) ) {
print
'<div align="left"><div id="h1">' . $data->title '</div></div><br><br><div id="postbody">' . '<img src="' . url($data->filepath) . '" />' . "<br>" . $data->body . "<br><br></div>";
}

?>

As I was getting some errors with the query. It still doesn't seem to be working.

I'll take a closer look at it.

Thanks again.

Sarah

this works

scarer - November 27, 2007 - 02:12

thanks for all your help nevets. i eventually got this to work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Current IT Newsletter | Printable version</title>
<style type="text/css" media="all">@import "misc/print.css";</style>
</head>

<body>
<div align="center">
<div id="banner"></div>
<?php

/*
Author: Sarah Vardy
Date: 20-11-2007
*/

static $matchCount = 0;
  

static
$counter = -1;

//get current month
$tmpMonth = date('m');
$tmpYear = date('Y');

static
$tmpTitleArray = array();

//time formats
$format = '%m-%Y';

//array of month values
$monthArray = array (January,February,March,April,May,June,July,August,September,October,November,December);
print
'<div id="masthead"><img src="newsletter/griffith_masthead.gif"></img></div>';

print
'<div align="left">Newsletter for the month of ' . $monthArray[$tmpMonth -1] . ', ' . $tmpYear . "<br><br></div>";

$nidArray = array();//array to store node ids of docs

//linking to the mysql database
$link = mysql_connect( "localhost", "rcsnews", "");
mysql_select_db("rcsnews2db", $link);


$result1 = mysql_query("SELECT created, body, node_revisions.title, node_revisions.nid, filepath FROM node LEFT JOIN node_revisions ON node.nid = node_revisions.nid LEFT JOIN files ON node_revisions.nid = files.fid WHERE type = 'book' GROUP BY node.nid");


while (
$node = mysql_fetch_object($result1)) {

  
$storeTime = ($node->created);
  
$tmpTime  = strftime($format, $storeTime);
  
$tmpBody = $node->body;
  
$tmpNid = $node->nid;
  
$tmpTitle = $node->title;
  
$currentTime = strftime($format, time());
  
$tmpFilepath = $node->filepath;
  
//call a function to process the results
  
$counter++;
  
processResults($tmpTime, $tmpBody, $currentTime, $tmpNid, $tmpFilepath, $counter, $tmpNidArray, $tmpTitle);

}

/* need to extract the current year and month and compare them to the year and month stored in the array */

function processResults($tmpTime, $tmpBody, $currentTime, $tmpNid, $tmpFilepath, $counter, $tmpNidArray, $tmpTitle)
{

  
//need to check if the document has been printed before
  
$tmpNidArray[$counter] = $tmpNid;
  
 
               if (
$tmpTime == $currentTime && $matchCount < 1)
            {
                   print
'<div align="left"><div id="h1">' . $tmpTitle '</div></div><br><br><div id="postbody">' . '<img src="' . $tmpFilepath . '" />' . "<br>" . $tmpBody . "<br><br></div>";
                print
$counter + 1;
               
$matchCount = 0;
               
           

            }
            else
            {
                print
'Not this doccy!';
               
//print $counter;
       
           
}
     

        return;
 

return;

}
//end processResults



mysql_close($link);

?>


</div>
</body>
</html>

Glad you got it working

nevets - November 27, 2007 - 03:58

Interesting the code did not work for you since I actually tested it and had it working. It would produce nothing if there where no book nodes with attachements but otherewise should work.

Note about your use of $tmpFilepath = $node->filepath; that needs to be at least $tmpFilepath = url($node->filepath); or the path will not always work (without the call to url() the file path will be relative to the pages url and not the sites root. To allow for cases where there is no file path you would want something like

if ( $node->filepath ) {
  $tmpFilepath = url($node->filepath);
}
else {
  $tmpFilepath = '';
}

thanks nevets

scarer - November 27, 2007 - 04:29

yeah it is weird. oh well at least it's working. that's the main thing =)

prserving formatting

scarer - November 27, 2007 - 06:07

just realized one major flaw - I'm losing all the formatting in the body. Do you know of a way to fix this?

It kind of depends on how much formatting you wish to retain

nevets - November 27, 2007 - 21:57

node_view() will get you a formatted version of the node (which will be more than the body).

If you only want the body formatted you could use

  $node = node_build_content($node);

  $content = drupal_render($node->content);

and use $content.

where does the content come from?

scarer - November 28, 2007 - 04:06

where does the content come from in the tables I am using? there is no column named 'content'.

node_build_content builds up $node->content

nevets - November 28, 2007 - 04:33

node_build_content builds up $node->content which is an array of view elements for the node. drupal_render() then takes this array and output html.

so do i need to include the two functions in my code?

scarer - November 28, 2007 - 05:57

do i just get the functions from somewhere in one of the modules?

Functions already defined

nevets - November 28, 2007 - 16:22

Both the functions are part of Drupal core, you just need to call them.

good learning curve but found some modules

scarer - November 29, 2007 - 03:01

just found some modules that do the job for what i want lol. it was a good learning curve anyway ;) thanks for all your help.

the printable module does a pretty good job for what i want yay!

thanks again,

sarah

 
 

Drupal is a registered trademark of Dries Buytaert.