SelfObscurity Posted May 26, 2007 Share Posted May 26, 2007 I have been working on for a few days now, getting an if statement to work with reading from a MySQL database. My goal is to have my web site to entirely run off index.php. I have taken a look at a very simple, manual-written if statement, and I am trying to incorporate it to work with MySQL. I will comment brlow what I don't sunderstand how to do. <?php // If the $page will == a tablename, is there a way I can have ti automatically choose the correct table, or will I have to specify it manually? For this line, I'm looking for $page == 'news' because I want to specifiy that the news table be selected. if ($page == 'one') { // I understand how to display infromation from the table, BUT...if index.php?page=news is displaying the news database, I want each news item to be clickable, to show more details. Would that end up being index.php?page=news&id=1 and if so, how can I specify the id veriable, while still jkeeping the page variable? include ("page1.php"); } elseif ($id == 'two') { include ("page2.php"); } else { include ("main.php"); } ?> I hope I made sense. If not, I can try and clairify things more. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/ Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 i think what your looking for is $_GET ie index.php?page=1&id=3 $_GET['page']; //returns 1 $_GET['id']; //returns 3 Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262271 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks, but what I don't understand, is how would I call for the ID, while still maintaining the PAGE? If I do an elseif, will it still understand I still want page too? I'm confused. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262274 Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 i'm sorry i don't understand.. your logic! Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262278 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 Let me see if I can get more clear. I know what I want to do, but I'm having problems wording it right. <?php if ($page == 'NewsTable') { echo("My code would go here, that would display all rows in the news table, basically showing all of the news posts. But, I also want to be able to allow each news post to be clickable, so a full, extended post can be seen. This would be done through the id field. So what I can't figure out, is with the already stated if statement, how can I make another ifstatement inside it, to represent the ID field...thus I will have index.php?page-news that will show all news posts AND index.php?page=news&id=1 that will shows an the news post at ID field 1 with some additional information about the post."); } elseif ($page == AnotherPage) { echo("More code to display table information"); } else echo("Default information displaying table information for homepage."); ?> Does this help any? Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262285 Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 OK i am still not 100% sure but let me know if i am close.. OK we click one News "Sports" section link = index.php?page=sport in this page it shows a summory of all the sports (SELECT ID, summery from news_table) IE 1. England win the world cup but if we click one the 1. (for example) it takes up to another page with the full news link = index.php?page=sport&id=1 Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262308 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 Exactally. I have been working at this all day and I am getting nowhere. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262309 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 I have attempted to get this going, but I'm not getting anywhere. This is the current code for what i am trying to do. <?php // Page information will go here if ($_GET['page'] == 'news') { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo ("<strong><span class=\"style1\">".$row['date']." - ".$row['heading']."</span></strong>"); } } elseif ($_GET['page'] == 'news' and $_GET['id'] == ".$row['heading'].") // when I put ".$row['heading']." in that string, it doesn't recognize that it is a fetch for the row, and I get a sql error...Parse error: syntax error, unexpected T_STRING in /home/jedney/public_html/beta/index2.php on line 118 { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo ("<p><strong><span class=\"style1\">".$row['heading']."</span></strong></p> <p align=\"left\" class=\"style1\">".$row['location']." ".$row['date']." - ".$row['extcontent']." <br /> </p> <hr /> </div>"); } else { echo("blah blah blah"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262315 Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 does each entry have an UID (unique ID) ? using header it would look somethink like this <?php // Page information will go here if ($_GET['page'] == 'news') { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo ("<a href='index.php?news={$_GET['page']}&header={$row['heading']}'><strong><span class=\"style1\">".$row['date']." - ".$row['heading']."</span></strong></a>"); } } if ($_GET['page'] == 'news' && isset($_GET['heading']) ) { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news WHERE heading = '{$_GET['heading']}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo ("<p><strong><span class=\"style1\">".$row['heading']."</span></strong></p> <p align=\"left\" class=\"style1\">".$row['location']." ".$row['date']." - ".$row['extcontent']." <br /> </p> <hr /> </div>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262318 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 Yes. The ID field is an auto_incriment field, rises by one with each post. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262319 Share on other sites More sharing options...
AndyB Posted May 26, 2007 Share Posted May 26, 2007 Let's assume (hope) that you have an id in the database table. In the clickable headings, use something like a href= ... .php?page=news&id=$row['id'] Then you can abstract the row id when the link is clicked and retrieve that particular record to show the whole news item Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262320 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 Post edited and removed... Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262325 Share on other sites More sharing options...
SelfObscurity Posted May 26, 2007 Author Share Posted May 26, 2007 I have altered my coding compared to what you posted MadTechie, but since adding the coding, I have ran into a basic Parse Error.. syntax error, unexpected $end in /home/jedney/public_html/beta/index2.php on line 143 I have looked over it, and emailed the page to a friend, and neither of us can find the problem... <?php // Page information will go here if ($_GET['page'] == 'news') { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo ("<a href='index.php?page=news&id={".$row['id']."}'><strong><span class=\"style1\">".$row['date']." - ".$row['heading']."</span></strong></a>"); } } if ($_GET['page'] == 'news' && isset($_GET['id']) ) { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news WHERE id = '{$_GET['id']}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo ("<p><strong><span class=\"style1\">".$row['heading']."</span></strong></p> <p align=\"left\" class=\"style1\">".$row['location']." ".$row['date']." - ".$row['extcontent']." <br /> </p> <hr /> </div>"); } else { include("main.php"); ?> That is the only PHP coding on the page, and main.php displays fine, so it's the index.php. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262331 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 missed the last } Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262340 Share on other sites More sharing options...
SelfObscurity Posted May 27, 2007 Author Share Posted May 27, 2007 I can't believe I missed that, although I have been looking at code all day. I have tried the new script of code, and the following has happened. 1. When index.php?page=news is loaded, it loades the contents of main.php too. 2. When I click on the link of the news article, it removes the contents of main.php, and just leaves the clickable link at the top. <?php // Page information will go here if ($_GET['page'] == 'news') { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo ("<a href='index2.php?page=news&id=".$row['id']."'><strong><span class=\"style1\">".$row['date']." - ".$row['heading']."</span></strong></a>"); } } elseif ($_GET['page'] == 'news' && isset($_GET['id']) ) { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news WHERE id = '{$_GET['id']}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo ("<p><strong><span class=\"style1\">".$row['heading']."</span></strong></p> <p align=\"left\" class=\"style1\">".$row['location']." ".$row['date']." - ".$row['extcontent']." <br /> </p> <hr /> </div>"); } else { include("main.php"); } ?> [\code] Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262354 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 Lets swap thing around a little OK first check if page is set then if ID show the id sectio else the summery section see below <?php // Page information will go here if ($_GET['page'] == 'news') { if (isset($_GET['id']) ) { mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news WHERE id = '{$_GET['id']}'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo ("<p><strong><span class=\"style1\">".$row['heading']."</span></strong></p> <p align=\"left\" class=\"style1\">".$row['location']." ".$row['date']." - ".$row['extcontent']." <br /> </p> <hr /> </div>"); }else{ mysql_connect($Host,$User,$Pass); @mysql_select_db($DB) or die ("Unable to select requested database."); $query="SELECT * FROM news"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo ("<a href='index2.php?page=news&id=".$row['id']."'><strong><span class=\"style1\">".$row['date']." - ".$row['heading']."</span></strong></a>"); } } } //no else needed as we always want main (i think) include("main.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262360 Share on other sites More sharing options...
SelfObscurity Posted May 27, 2007 Author Share Posted May 27, 2007 Thank you so very much, it works perfectly. Of all of the things I have looked at on this, I never read anything on isset. I'm a work in-progress. I appreciate the time you took to help me with this. A gentleman of scholar. Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262375 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 your very welcome, you wanted to learn so i helped Quote Link to comment https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/#findComment-262377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.