Archive for the ‘PHP’ Category

7. Formatting complex MySQL query in PHP

7 September 2008

Ever since LabourStart was founded we’ve had top priority news stories in English followed by lesser priority ones, but have never done this for our other 21 languages.  Until now.

Now we have the capacity to do this and I’ve added some lines of code to our PHP script that generates our front pages — but it’s not working.  I run a PHP function (readdb) twice, like this:

readdb (3,5);
readdb (2,100);

The first number is the story’s priority (taken from our database) and the second is the maximum number of stories to show.

Then I call the function like this:

function readdb ($ipriority,$totalstories) {

But it’s not working as I’d hoped.

This is really a matter of getting the syntax right — and nothing I’m trying seems to work.

I want to tell the system to get all records from a table called “news” where the priority is either 2 or 3 and where the language code is a 2 or 3 character code, and then to sort by date and within date, by the date and time the story was entered into our database.  This is the line that’s causing all the problems — remember that priority is a numeric field and language is text:

$query = “SELECT * FROM news WHERE priority = $ipriority AND language = ‘$langcode’ ORDER BY date DESC, dateposted DESC LIMIT $totalstories”;

Can anyone help?  Thanks!

5. Strange behaviour on our new Farsi (Persian) page

3 September 2008

First of all, you don’t need to know Farsi to help out here.  I don’t understand Farsi myself, but the problem is obvious when you look at the page.  Here’s a screenshot of the problem:

The problems are in the first and last rows.  Reading from right to left (and the HTML header instructs the page to be read from right to left), you should see the country name, the header (in Farsia), the date and then in brackets, the source.

Here is the HTML header:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fa" lang="fa" dir="rtl">

As you can plainly see, the first line is screwed up because the closing bracket for the source (IASWI) goes after the final field, which is a link to a campaign and is highlighted in yellow.

The last line is even worse – it puts the source first, the country last.  This should have nothing to do with the fact that the text is in English — because it’s tagged as being in Farsi (the link actually takes you to a page with both Farsi and English text).

Here is the PHP code for generating this line on our page:

echo (‘<a href=”http://www.labourstart.org/cgi-bin/show_news.pl?country=’);
echo $countryen;
echo (‘”><b>’);
echo $country;
echo (‘</b></a> ‘);
echo (‘<a href=”‘);
echo $url;
echo (‘” title=”‘);
echo $userid;
echo (‘”>’);
echo $header;
echo (‘</a> ‘);
if ($row2['actnowcampaigncode'] > 0) {
echo (‘ <a href=”http://www.labourstart.org/cgi-bin/solidarityforever/show_campaign.cgi?c=’);
echo $actnowcampaigncode;
echo (‘”><span style=”background-color:yellow;color:black”><b>Act</b><i>NOW!</i></span></a> ‘);
}
echo $dd;
echo (‘-’);
echo $mm;
echo (‘-’);
echo $yyyy;
echo (‘ [');
echo $source;
echo ('] ‘);

Anyone have any ideas about how to fix this?  Thanks.

3. A simple MySQL sorting question [SOLVED]

3 September 2008

CASE CLOSED.  The solution was partially implemented and is now fully working.  There is a MySQL field called dateposted.  I have changed this to be a DATETIME field.  The sort is now by date DESC, dateposted DESC.  This should all work.  Thanks for getting those grey cells working!

In the PHP code for the page which shows our news in Norwegian, we sort in descending order based on the date field.  The problem is that this means that the newest items added to the database  within a single date are moved to the bottom.  How can we keep the newest items on top?

Here is the code we’re using:

$query2 = “SELECT * FROM news WHERE language = ‘no’  order by date DESC LIMIT 100″;