SQL is being stupid

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

SQL is being stupid

Post 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 <img src=\'http://www.killanet.net/forum3/public/s ... ictory.gif\' class=\'bbc_emoticon\' alt=\':victory:\' />


[/font][/size]
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

SQL is being stupid

Post 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]
Last edited by Josh on Tue Sep 20, 2005 2:13 am, edited 1 time in total.
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
radar
Hero Member
Hero Member
Posts: 632
Joined: Thu Jun 17, 2004 12:37 am

SQL is being stupid

Post 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]
Last edited by radar on Tue Sep 20, 2005 12:36 pm, edited 1 time in total.
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

SQL is being stupid

Post 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.
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

SQL is being stupid

Post 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 <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

SQL is being stupid

Post by Josh »

lol yanno... I\'m just as confused as you <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' /> How about I just start over. <img src=\'http://www.killanet.net/forum3/public/s ... iggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' /> yes... we\'ll do that. No multi deminsional arrays or whatever. <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
Last edited by Josh on Tue Sep 20, 2005 8:42 pm, edited 1 time in total.
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
radar
Hero Member
Hero Member
Posts: 632
Joined: Thu Jun 17, 2004 12:37 am

SQL is being stupid

Post 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.
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

SQL is being stupid

Post by Josh »

Hmmmm... You just gave me an idea radar, thanks <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Post Reply

Return to “PHP &amp; MySQL”