Jump to content

Archived

This topic is now archived and is closed to further replies.

me1000

display orginized mysql query

Recommended Posts

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

Share this post


Link to post
Share on other sites
Use a while loop, and make the query be ordered by ASC or DESC meaning, Ascending or Descending.

Share this post


Link to post
Share on other sites
thanks for that heres what I came up with, only thing is it doesnt work! lol

[code]
<?
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>";

?>[/code]

See anything wrong?


Never Mind I fixed it!!!

thanks for all your help!

Share this post


Link to post
Share on other sites
Ok now I need help with the link to edit

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

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
[code]
$edit = '<a href ="cms.php?page_id=$page_id">Edit Page</a> '
[/code]

but no cigar


this is my full page if it helps.

[code]
<?
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>";

?>

[/code]

Share this post


Link to post
Share on other sites
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> '

Share this post


Link to post
Share on other sites
Thanks for that it works now. :)

now its time to make it more complex lol!


Thanks again!

Share this post


Link to post
Share on other sites
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

[code]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";
    }[/code]
now the problem is that im using
[code]echo "<TR><TD>$page_type</TD><TD>$page_title</TD><TD>$page_id</TD><TD>$edit</TD></TR>";[/code]

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.

Share this post


Link to post
Share on other sites
change this:
if ($page_type == "1")


to this:
if ($page_type == 1)


Share this post


Link to post
Share on other sites
ok I chaged it now the if statments working right but its not putting it in the table right.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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,

[code]<?
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>";

?>[/code]



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

Share this post


Link to post
Share on other sites
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>";

?>

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks A million that fixed it. I got to remember that now. lol

I really appreciate your help!

Share this post


Link to post
Share on other sites
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:
[code]
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;
}
[/code]


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



and you could finish it off with the others :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.