Jump to content

[SOLVED] If Statements w/MySQL DB


SelfObscurity

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/53092-solved-if-statements-wmysql-db/
Share on other sites

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?

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

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");
?>

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>");
}

?>

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

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.

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]

 

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");
						  
?>

Archived

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

×
×
  • 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.