Jump to content


Photo

display orginized mysql query


  • Please log in to reply
17 replies to this topic

#1 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 05 March 2006 - 06:39 PM

ok I have a my sql table set up as so...

+---+---------------+---------------+---------+
| ID | PAGE_TITLE | PAGE_TYTLE | Conent |
+---+---------------+---------------+---------+
| 1 | title | type | Conent |
+---+---------------+---------------+---------+
| 2 | title2 | type2 | Conent |
+---+---------------+---------------+---------+

in doing a mysql query select * from $tablename

so I want to have a page that sets up a simple table that show the page Title, then the ID next to it in a diffrent column. beside all that I want another collumn that says edit in every row. this will link to page.php?pag_id=2
where 2 is the ID of the page.

i am clueless on how to do this. though, i tried to explain it as well as I could I hope you understand.


how would you do it? Right now i dont have any idea!!!

Thanks

#2 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 06 March 2006 - 01:07 AM

Use a while loop, and make the query be ordered by ASC or DESC meaning, Ascending or Descending.

#3 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 02:06 AM

thanks for that heres what I came up with, only thing is it doesnt work! lol

<?
require_once ("config.php");

$sql = "SELECT ID, PAGE_TITLE, PAGE_TYPE
FROM $sTableName
ORDER BY PAGE_TYPE DESC";

$sql_result = mysql_query($sql);
 
echo "<TABLE BORDER=1>";
echo "<TR><TH>Page Type </TH><TH>Page Title </TH><TH>Page ID </TH>";
while ($row = mysql_fetch_array($sql_result)) {
$page_id = $row["ID"];
$page_title = $row["PAGE_TITLE"];
$page_type = $row["PAGE_TYPE"];
echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD></TR>";
}
echo "</TABLE>";

?>

See anything wrong?


Never Mind I fixed it!!!

thanks for all your help!

#4 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 02:18 AM

Ok now I need help with the link to edit

$edit = 'echo = "<a href ="cms.php?page_id=$page_id">Edit Page</a>" '

Im not sure where I should add the quotes and what type of quotes to add, since there is a variable in the URL

{edit}
gettin closer
$edit = '<a href ="cms.php?page_id=$page_id">Edit Page</a> '

but no cigar


this is my full page if it helps.

<?
require_once ("config.php");

$sql = "SELECT ID, PAGE_TITLE, PAGE_TYPE
FROM $sTableName
ORDER BY PAGE_TYPE ASC";

$sql_result = mysql_query($sql);
 
echo "<TABLE BORDER=1>";
echo "<TR><TH>Page Type </TH><TH>Page Title</TH><TH>Page ID</TH><TH>Edit Page</TH>";
while ($row = mysql_fetch_array($sql_result)) {


$page_id = $row["ID"];
$page_title = $row["PAGE_TITLE"];
$page_type = $row["PAGE_TYPE"];
$edit = '<a href ="cms.php?page_id='$page_id'">Edit Page</a>';


echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";
}
echo "</TABLE>";

?>



#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 02:24 AM

make the table outside the loop, then inside, make the columns and rows

try this


$edit = '<a href ="cms.php?page_id='.$page_id.'">Edit Page</a> '
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 02:30 AM

Thanks for that it works now. :)

now its time to make it more complex lol!


Thanks again!

#7 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 02:31 AM

lol, anytime.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#8 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 02:54 AM

ok instead of page type being a number which it is in the database, I want to convert it to text. Im using a if statment for this

if ($page_type == "1") {
    echo "Home";
    } else if ($page_type == "2") {
    echo "Series";
    } else if ($page_type == "3") {
    echo "Movies";
    } else if ($page_type == "4") {
    echo "Special";
    } else if ($page_type == "5") {
    echo "Gaming";
    } else {
    echo "unknown";
    }
now the problem is that im using
echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";

to show the each row. yet that doesnt get me much if you know what I mean.
how do I do this?? I will probably need to change the variable name in the table.

#9 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 03:01 AM

change this:
if ($page_type == "1")


to this:
if ($page_type == 1)



regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#10 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 03:04 AM

ok I chaged it now the if statments working right but its not putting it in the table right.

#11 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 03:06 AM

Im not sure what your problem is.

You got all those IF statements to work but removing the"" from each number, right?

What was the other problem?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#12 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 03:10 AM

ok inside the mysql table the values for PAGE_TYPE are numbers, the if statment converts them all to text
but I want them to show up in the table

heres my page again,

<?
require_once ("config.php");

$sql = "SELECT ID, PAGE_TITLE, PAGE_TYPE
FROM $sTableName
ORDER BY PAGE_TYPE ASC";

$sql_result = mysql_query($sql);
 
echo "<TABLE BORDER=1>";
echo "<TR><TH>Page Type </TH><TH>Page Title</TH><TH>Page ID</TH><TH>Edit Page</TH>";
while ($row = mysql_fetch_array($sql_result)) {


$page_id = $row["ID"];
$page_title = $row["PAGE_TITLE"];
$page_type = $row["PAGE_TYPE"];
$edit = '<a href ="cms.php?page_id='.$page_id.'">Edit Page</a>';

// change page type #'s to words

if ($page_type == 1) {
    echo "Home";
    } else if ($page_type == 2) {
    echo "Series";
    } else if ($page_type == 3) {
    echo "Movies";
    } else if ($page_type == 4) {
    echo "Special";
    } else if ($page_type == 5) {
    echo "Gaming";
    } else {
    echo "unknown";
    }


echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";
}
echo "</TABLE>";

?>



I think the if statment may need to replace the $page_type variable in the

echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";

But im not sure on how to add it to there

#13 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 03:15 AM

Try this:


<?
require_once ("config.php");

$sql = "SELECT ID, PAGE_TITLE, PAGE_TYPE
FROM $sTableName
ORDER BY PAGE_TYPE ASC";

$sql_result = mysql_query($sql);

echo "<TABLE BORDER=1>";
echo "<TR><TH>Page Type </TH><TH>Page Title</TH><TH>Page ID</TH><TH>Edit Page</TH>";
while ($row = mysql_fetch_array($sql_result)) {


$page_id = $row["ID"];
$page_title = $row["PAGE_TITLE"];
$page_type = $row["PAGE_TYPE"];
$edit = '<a href ="cms.php?page_id='.$page_id.'">Edit Page</a>';

// change page type #'s to words
$new_type = NULL;

if ($page_type == 1) {
$new_type = "Home"
echo $new_type;
} else if ($page_type == 2) {
$new_type = "Series"
echo $new_type;
} else if ($page_type == 3) {
$new_type = "Movies"
echo $new_type;
} else if ($page_type == 4) {
$new_type = "Special"
echo $new_type;
} else if ($page_type == 5) {
$new_type = "Gaming"
echo $new_type;
} else {
$new_type = "Unknown"
echo $new_type;
}


echo "<TR><TD>$new_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";
}
echo "</TABLE>";

?>
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#14 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 03:19 AM

im getting a phrase error on line 25.

Parse error: parse error, unexpected T_ECHO in /htdocs/cms/pages.php on line 25

line 25 is the first

echo $new_type;

#15 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 03:21 AM

Oh sorry.

I forgot to put ; after some lines


$new_type = "Home"


should be
$new_type = "Home";



and for all the others below that too lol. sorry
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#16 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 03:24 AM

Thanks A million that fixed it. I got to remember that now. lol

I really appreciate your help!

#17 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 03:29 AM

Anytime.
While im at it, let me give you some more advice.

Instead of using a bunch of IF/ELSE IF statements like you did, use the SWITCH statement.
I'm not sure if it makes your code faster or not, but it makes it easier to read and apparently, its more programmer-like (in other words, looks less newb :) )

so instead of:
if ($page_type == 1) {
$new_type = "Home"
echo $new_type;
} else if ($page_type == 2) {
$new_type = "Series"
echo $new_type;
} else if ($page_type == 3) {
$new_type = "Movies"
echo $new_type;
} else if ($page_type == 4) {
$new_type = "Special"
echo $new_type;
} else if ($page_type == 5) {
$new_type = "Gaming"
echo $new_type;
} else {
$new_type = "Unknown"
echo $new_type;
}


You could/should have:
$new_type = NULL;
switch ($page_type) {
    case 1:
        $new_type = "Home";
        echo $new_type;
        break;
          
     case 2:
        $new_type = "Series";
        echo $new_type;
        break;
  



and you could finish it off with the others :)
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#18 me1000

me1000
  • Members
  • PipPipPip
  • Advanced Member
  • 183 posts

Posted 06 March 2006 - 03:38 AM

Cool Thanks Ill keep that in mind!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users