Jump to content

display orginized mysql query


me1000

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
Link to comment
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!
Link to comment
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]
Link to comment
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.
Link to comment
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
Link to comment
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>";

?>
Link to comment
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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.