Page 1 of 1

SQL is being stupid

Posted: Mon Sep 19, 2005 10:54 pm
by Josh
[size=1][font=\"Trebuchet Ms\"]

Hello,
If anyone could help me or something it would be greatly appreciated. I\'ve been getting help from the penguin (ner0) and we\'ve come up with this code and it\'s not working the way I want it and at the moment I can\'t get ahold of paul. So here goes the explaination of what it should be doing.

This code *should* display 3 of the latest entries from the database inside of a table. This will work for every category I have submitted into the database (The category is submitted inside the table the news is inserted into). I hope this all makes sense. Here is the code.

[code]<?php

$sql_host = "localhost";
$sql_user = "*";
$sql_pass = "**";
$sql_db = "news";

$conn = @mysql_connect($sql_host, $sql_user, $sql_pass) or die("could not connect. mysql said: <br /><br />" . mysql_error());
$select = mysql_select_db($sql_db) or die("cannot select database. mysql said:<br /><br />" . mysql_error());


$query = mysql_query("select * from `news` order by `cat` asc");
$table = array();
while ($temp=mysql_fetch_array($query))
{
$table[$temp[\'cat\']][] = $temp[\'id\'];
}
echo "<table border=1px>\n";
echo " <tr>\n";
// how many columns per row?
$col = 2;
$i = 0;
foreach($table as $key => $val)
{


if ($i === $col)
{
echo " </tr>\n";
echo " <tr>\n";
$i = ($i - 2);
}
echo " <td>\n";
echo " <b>category:</b>{$key}<br />\n";

//------------------
// for each id in this category...
//------------------
foreach ($val as $subval)
{

//------------
// fetch details based on the current id (we\'re looping thru\' \'em)
//------------

$query = mysql_query("select * from `news` where id=\'$subval\' limit 3");
$temp = mysql_fetch_array($query);

//------------
// output details:
//------------

echo " <i>title:</i> {$temp[\'title\']} <br />\n";
}
echo " </td>\n";
$i++;
}
echo "</table>\n";[/code]

Notice how I\'m using a query near the bottom of the script where it should LIMIT 3, but it isn\'t.

Thanks in advanced,
-Dren /victory.gif\' class=\'bbc_emoticon\' alt=\':victory:\' />


[/font][/size]

SQL is being stupid

Posted: Tue Sep 20, 2005 2:12 am
by Josh
Okay update... I fixed it so here is the new code. It just isn\'t doing what I want now..... aka display the category in a cell and a list of the three latest news entries under it... :\

[code]<?php

$sql_host = "*";
$sql_user = "**";
$sql_pass = "";
$sql_db = "news";

$conn = @mysql_connect($sql_host, $sql_user, $sql_pass) or die("could not connect. mysql said: <br /><br />" . mysql_error());
$select = mysql_select_db($sql_db) or die("cannot select database. mysql said:<br /><br />" . mysql_error());

$query2 = mysql_query("select * from `news` limit 3");
$query = mysql_query("select * from `news` order by `cat` asc");
$table = array();
while ($temp=mysql_fetch_array($query))
{
$table[$temp[\'cat\']][] = $temp[\'id\'];

}

echo "<table border=1px>\n";
echo " <tr>\n";

// how many columns per row?
$col = 2;
$i = 0;
foreach($table as $key => $val)
{
if ($i === $col)
{
echo " </tr>\n";
echo " <tr>\n";
$i = ($i - 2);
}

echo " <td>\n";
echo " <b>category:</b>{$key}<br />\n";

//------------------
// for each id in this category...
//------------------
foreach ($val as $subval)
{
//------------
// fetch details based on the current id (we\'re looping thru\' \'em)
//------------

//$query2 = mysql_query("select * from `news` where id=\'$subval\' limit 3");
$temp = mysql_fetch_array($query2);

//------------
// output details:
//------------
if ($temp[\'title\']) {

echo " >>> {$temp[\'title\']} <br />\n";
}
}

echo " </td>\n";
$i++;
}

echo "</table>\n";[/code]

SQL is being stupid

Posted: Tue Sep 20, 2005 10:22 am
by radar
So it's a basic news script?
[code]<?
$sql_host = "*";
$sql_user = "**";
$sql_pass = "";
$sql_db = "news";
//assuming your table is called news also...
$q = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0 , 3");
/*The line selects all fields from news and then orders them by ID. Basically, if you have an auto-numerical
system for your IDs, then the highest ID is going to be the first post shown. Lets say you have a news post
titled "post one" with the ID of 84 and another with the ID of 83 titled "post two". Post one will be shown
before post two*/
while ($a = mysql_fetch_array($q)) {
echo "insert table or whatever here";
//variables are $a[fieldname]
}

?>[/code]

[color=\"green\"][size=1]edit the vercz way: fixed tags[/size][/color]

SQL is being stupid

Posted: Tue Sep 20, 2005 11:21 am
by Josh
Yes it\'s a news script, but you also have to grab the category which is with the news... So I have to grab the category and make sure the news is sorted into the right category... And it\'s not doing that now. It used to, but it doesn\'t now.

SQL is being stupid

Posted: Tue Sep 20, 2005 4:10 pm
by Scott
I\'m a bit confused as what your trying to get the code to do. Can you paste an example of the table your querying? Particularly confused about the use of ID. Is it different for every entry, or different for every category? Can a category have more than one news report in it?

Em..I think thats all /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />

SQL is being stupid

Posted: Tue Sep 20, 2005 8:42 pm
by Josh
lol yanno... I\'m just as confused as you /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' /> How about I just start over. /biggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' /> yes... we\'ll do that. No multi deminsional arrays or whatever. /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />

SQL is being stupid

Posted: Wed Sep 21, 2005 9:38 am
by radar
You want it ordered by the category and the ID? Easy.

$q = mysql_query("SELECT * FROM `news` ORDER BY `catid` DESC AND ORDER BY `id` DESC");

Not quite sure if it will work or not.

SQL is being stupid

Posted: Wed Sep 21, 2005 11:11 am
by Josh
Hmmmm... You just gave me an idea radar, thanks /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />