creating 2 column results mysql php

He has: 31 posts

Joined: Jan 2004

HI all..

OK what I'm trying to do is instead of a single row of results show 2 or even 3 rows of database results.

some say its simple but being self taught the only help i get is from example scripts and here.

my page is below if someone can help it would be appreciated.

if (file_exists('../library/config.php')) {
include("../library/config.php");
} else {
include("library/config.php");
}

if (file_exists('../library/opendb.php')) {
include("../library/opendb.php");
} else {
include("library/opendb.php");
}
$ASC = 'ASC';
$query="SELECT * FROM gallery_category ORDER BY category_name ASC";
$result=mysql_query($query);

if (!$result==1) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

while ($gal = mysql_fetch_assoc($result)) {
myarray=array();
$j=1;
while (next-record-loop)
{
myarray+=array($j=>array('category_id'=>category_id, 'category_name'=>category_name, 'cat_description'=>cat_description));
$j++;
}
echo "";
echo $gal['category_name'];
echo "";
echo nl2br($gal['cat_description']);
echo "";
}
themesidebox3();
if (file_exists('../library/closedb.php')) {
include("../library/closedb.php");
} else {
include("library/closedb.php");
}

Busy's picture

He has: 6,151 posts

Joined: May 2001

Some of the code above you dont need

if (file_exists('../library/opendb.php')) {
include("../library/opendb.php");
} else {
include("library/opendb.php");
}
$ASC = 'ASC';
$query="SELECT * FROM gallery_category ORDER BY category_name ASC";
$result=mysql_query($query);

if (!$result==1) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

while ($gal = mysql_fetch_assoc($result)) {
myarray=array();
$j=1;
while (next-record-loop)
{

myarray+=array($j=>array('category_id'=>category_id, 'category_name'=>category_name, 'cat_description'=>cat_description));
$j++;
}

I have made the extra bits bold. you don't need the inner while loop. if you want to limit the results, say you have 100 items in your database but only want 10, you'd use limit and even offset

LIMIT 10 // gets first ten results
LIMIT 5,10 // gets 5 results starting at 10 (really 9 as mysql starts at 0)

He has: 31 posts

Joined: Jan 2004

the highlighted line below is causing a pharse error now...

Parse error: parse error, unexpected '=' in /**/**/**/**/**/**/**/index.php on line 31

been sat here looking at other scripts and example sites and still cant figure this one out..

---------------------------------------------------------------------
if (!$result==1) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

while ($gal = mysql_fetch_assoc($result)) {
[****line 31***]myarray=array();[***line 31***]]
myarray+=array($j=>array('category_id'=>category_id, 'category_name'=>category_name, 'cat_description'=>cat_description));

echo "";
echo $gal['category_name'];
echo "";
echo nl2br($gal['cat_description']);
echo "";
------------------------------------------------------------------

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Looks like the while loop was opened { but not closed }.

He has: 31 posts

Joined: Jan 2004

mairving wrote: Looks like the while loop was opened { but not closed }.

no it does have closure it is just below the last line in the code above.

}

this is not easy like some people say Sad

Busy's picture

He has: 6,151 posts

Joined: May 2001

myarray=array();

should be

$myarray=array();

(needs the dollar sign) but really that bit and the following bit

myarray+=array($j=>array('category_id'=>category_id, 'category_name'=>category_name, 'cat_description'=>cat_description));

can/should be removed as they aren't doing anything, the results are coming from the database query loop.

If you leave it, the myarray+= needs a dollar sign too.
just comment it out for now even

// single line comment
/* multi
line
comment */
# to end of line comment (not recommend if you edit in different editors)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.