(Solved) A Cry For Help (using links to set variable)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

(Solved) A Cry For Help (using links to set variable)

Post by Infinity »

Hi i'm new to these forums and to php and infact all web related stuff and I need help I have some php code i've thrown together through trial and error.
I want to use 1 dynamic php page to access a mysql database which i have achieved (don't ask me how) the header of the page has some links I.E. 1952,1953 etc. the sql query is set to retreive all records from the database that have 1952 in the year field what I want to do is when I click on a link instaed of loading another page I want it to change the sql query say from

Code: Select all

<?php $query = "SELECT * FROM numberones  WHERE `year` = '1952'";?>
to

Code: Select all

<?php $query = "SELECT * FROM numberones  WHERE `year` = '1953'";?>
below is the complete code it probably has tons of stuff that need'nt or should'nt be there but I am a complete novice so any ideas on cleaning it up would also be appreciated.
This is the header music_header.tpl

Code: Select all

style type="text/css"&amp;gt;
&amp;lt;!--
@import url("../phpbb2/templates/fisubsilversh/fisubsilversh.css");
--&amp;gt;
&amp;lt;/style&amp;gt;
&amp;lt;table width="100%" height="100%"  border="0" cellpadding="2" cellspacing="6"&amp;gt;
  &amp;lt;tr&amp;gt;
    &amp;lt;td align="center" valign="top"&amp;gt;&amp;lt;TABLE class=bodyline cellSpacing=0 cellPadding=0 width="100%" border=0&amp;gt;
      &amp;lt;TBODY&amp;gt;
        &amp;lt;TR&amp;gt;
          &amp;lt;TD&amp;gt;
            &amp;lt;TABLE class=topbkg cellSpacing=0 cellPadding=0 width="100%" border=0&amp;gt;
              &amp;lt;TBODY&amp;gt;
                &amp;lt;TR&amp;gt;
                  &amp;lt;TD&amp;gt;&amp;lt;A href="http://127.0.0.1/phpBB2/index.php"&amp;gt;&amp;lt;IMG 
            title="Infinity's Total Music Forum Index" height=110 
            alt="Infinity's Total Music Forum Index" 
            src="phpBB2/templates/fisubsilversh/images/phpbb2_logo.jpg" width=240 
            border=0&amp;gt;&amp;lt;/A&amp;gt;&amp;lt;/TD&amp;gt;
                  &amp;lt;TD align=right width="100%"&amp;gt;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/redirect.php?banner_id=1" 
            target=_blank&amp;gt;&amp;lt;IMG 
            src="phpBB2/images/banner/phpbb2_banner.gif" 
            alt="www.phpbb2.de - No.1" width="468" height="60" 
            border=0 title="www.phpbb2.de - No.1"&amp;gt;&amp;lt;/A&amp;gt;&amp;lt;/TD&amp;gt;
                  &amp;lt;TD&amp;gt;&amp;lt;IMG height=4 alt="" 
            src="Infinity's Total Music  Home_files/spacer.gif" 
        width=28&amp;gt;&amp;lt;/TD&amp;gt;
                &amp;lt;/TR&amp;gt;
              &amp;lt;/TBODY&amp;gt;
            &amp;lt;/TABLE&amp;gt;
            &amp;lt;TABLE cellSpacing=0 cellPadding=2 width="100%" border=0&amp;gt;
              &amp;lt;TBODY&amp;gt;
                &amp;lt;TR&amp;gt;
                  &amp;lt;TD class=topnav align=middle&amp;gt;&amp;amp;nbsp;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/portal.php"&amp;gt;Home&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp; &amp;lt;A 
            href="http://127.0.0.1/phpBB2/search.php"&amp;gt;1952&amp;lt;/A&amp;gt; &amp;amp;nbsp;• &amp;amp;nbsp;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/memberlist.php"&amp;gt;1953&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/statistics.php"&amp;gt;1954&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A href="http://127.0.0.1/phpBB2/album.php"&amp;gt;1955&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A href="http://127.0.0.1/phpBB2/links.php"&amp;gt;1956&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/calendar.php"&amp;gt;1957&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A href="http://127.0.0.1/phpBB2/recent.php"&amp;gt;1958&amp;lt;/A&amp;gt; &amp;amp;nbsp; • &amp;amp;nbsp;&amp;lt;A 
            href="http://127.0.0.1/phpBB2/nickpage.php?admin=Infinity"&amp;gt;1959&amp;lt;/A&amp;gt; &amp;lt;BR&amp;gt;
    &amp;amp;nbsp;&amp;lt;A href="http://127.0.0.1/phpBB2/staff.php"&amp;gt;Staff Site&amp;lt;/A&amp;gt;&amp;amp;nbsp; • &amp;amp;nbsp;
                &amp;lt;/TR&amp;gt;
              &amp;lt;/TBODY&amp;gt;
            &amp;lt;/TABLE&amp;gt;
            &amp;lt;TABLE class=tbl cellSpacing=0 cellPadding=0 border=0&amp;gt;
              &amp;lt;TBODY&amp;gt;
                &amp;lt;TR&amp;gt;
                  &amp;lt;TD class=tbll&amp;gt;&amp;lt;IMG height=4 alt="" 
            src="Infinity's Total Music  Home_files/spacer.gif" width=8&amp;gt;&amp;lt;/TD&amp;gt;
                  &amp;lt;TD class=tblbot&amp;gt;&amp;lt;IMG height=4 alt="" 
            src="Infinity's Total Music  Home_files/spacer.gif" width=8&amp;gt;&amp;lt;/TD&amp;gt;
                  &amp;lt;TD class=tblr&amp;gt;&amp;lt;IMG height=4 alt="" 
            src="Infinity's Total Music  Home_files/spacer.gif" 
        width=8&amp;gt;&amp;lt;/TD&amp;gt;
                &amp;lt;/TR&amp;gt;
              &amp;lt;/TBODY&amp;gt;
            &amp;lt;/TABLE&amp;gt;
            &amp;lt;!-- The javascript presents here comes from MyCalendar 2.2.6 from MojavLinux --&amp;gt;
            &amp;lt;SCRIPT language=Javascript type=text/javascript&amp;gt;
&amp;lt;!--
var agt = navigator.userAgent.toLowerCase();
var originalFirstChild;

function createTitle(which, string, x, y)
&amp;#123;
    // record the original first child (protection when deleting)
    if (typeof(originalFirstChild) == 'undefined')
	&amp;#123;
        originalFirstChild = document.body.firstChild;
    &amp;#125;

    x = document.all ? (event.clientX + document.body.scrollLeft) : x;
    y = document.all ? (event.clientY + document.body.scrollTop) : y;
    element = document.createElement('div');
    element.style.position = 'absolute';
    element.style.zIndex = 1000;
    element.style.visibility = 'hidden';
    if (document.all)
	&amp;#123;
        element.style.width = '200px';
        excessWidth = 50;
        excessHeight = 20;
    &amp;#125;
    else
	&amp;#123;
        excessWidth = 0;
        excessHeight = 20;
    &amp;#125;
    element.innerHTML = '&amp;lt;div class="bodyline" style="padding: 2px; text-align: left;"&amp;gt;&amp;lt;span class="gen"&amp;gt;' + string + '&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;';
    renderedElement = document.body.insertBefore(element, document.body.firstChild);
    renderedWidth = renderedElement.offsetWidth;
    renderedHeight = renderedElement.offsetHeight;
    // fix overflowing off the right side of the screen
    overFlowX = x + renderedWidth + excessWidth - document.body.offsetWidth;
    x = overFlowX &amp;gt; 0 ? x - overFlowX : x;
    // fix overflowing off the bottom of the screen
    overFlowY = y + renderedHeight + excessHeight - window.innerHeight - window.pageYOffset;
    y = overFlowY &amp;gt; 0 ? y - overFlowY : y;
    renderedElement.style.top = (y + 15) + 'px';
    renderedElement.style.left = x + 'px';
    // windows versions of mozilla are like too fast here...we have to slow it down
    if (agt.indexOf('gecko') != -1 &amp;amp;&amp;amp; agt.indexOf('win') != -1)
	&amp;#123;
        setTimeout("renderedElement.style.visibility = 'visible'", 1);
    &amp;#125;
    else
	&amp;#123;
        renderedElement.style.visibility = 'visible';
    &amp;#125;
&amp;#125;

function destroyTitle()
&amp;#123;
    // make sure we don't delete the actual page contents (javascript can get out of alignment)
    if (document.body.firstChild != originalFirstChild)
	&amp;#123;
        document.body.removeChild(document.body.firstChild);
    &amp;#125;
&amp;#125;
//--&amp;gt;
    &amp;lt;/SCRIPT&amp;gt;
            &amp;lt;style type="text/css"&amp;gt;
&amp;lt;!--
body &amp;#123;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
&amp;#125;
.style1 &amp;#123;
	color: #0066CC;
	font-weight: bold;
&amp;#125;
.style2 &amp;#123;color: #DBF9F9&amp;#125;
.style3 &amp;#123;font-size: 24px&amp;#125;
.style4 &amp;#123;
	color: #FF6633;
	font-weight: bold;
&amp;#125;
.style5 &amp;#123;
	font-size: 36px;
	font-weight: bold;
	color: #FF9933;
&amp;#125;
.style7 &amp;#123;
	font-size: 12px;
	font-family: Arial, Helvetica, sans-serif;
	color: #3366CC;
&amp;#125;
.style8 &amp;#123;color: #FF6633&amp;#125;
a:link &amp;#123;
	text-decoration: none;
&amp;#125;
a:visited &amp;#123;
	text-decoration: none;
&amp;#125;
a:hover &amp;#123;
	text-decoration: none;
&amp;#125;
a:active &amp;#123;
	text-decoration: none;
&amp;#125;
--&amp;gt;
    &amp;lt;/style&amp;gt;
This is the body Index.php

Code: Select all

<?php include("music_header.tpl") ?>
<?php

	$location = "localhost";
	$username = "username";
	$password = "password";
	$database = "MYMUSIC";
	$conn = mysql_connect("$location","$username","$password");
	$year="1952";
	
	if (!$conn) die ("Could not connect MySQL");
	mysql_select_db($database,$conn) or die ("Could not open database");

$query = "SELECT * FROM numberones  WHERE `year` = '1952'";
	$result = mysql_query($query);
	$numrows = mysql_num_rows($result);




echo "<table width='100%'  border='1' cellpadding='2' cellspacing='2' bordercolor='#006699' align='center'>"; 
echo "<tr>";
echo "<td width='100%' background='../phpbb2/templates/fisubsilversh/images/cellpic3.gif'><div align='center' class='style2 style3'><strong>
          $year </strong></div></td>";
echo "</tr>";
echo "</table>";
echo "<table width='100%'  border='1' cellpadding='2' cellspacing='2' bordercolor='#006699' align='center'>"; 




echo "<td width='13%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Date</div></td>";
echo "<td width='33%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Artist</div></td>";
echo "<td width='39%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Title</div></td>";
echo "<td width='6%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Wks</div></td>";
echo "<td width='9%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>d/l</div></td>";	

	
	while($row = mysql_fetch_array($result))

 

{
		echo "<tr><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[year]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[artist]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[title]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[weeksatone]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[dllink]</div></td></tr>";
} 



echo "</table>"; 

?>
<?php include("music_footer.tpl") ?>

?>
Last edited by Infinity on Mon Feb 02, 2004 2:07 pm, edited 2 times in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Re: A Cry For Help

Post by qads »

here you go:

body Index.php

Code: Select all

<?php include("music_header.tpl") ?>
<?php

	$location = "localhost";
	$username = "username";
	$password = "password";
	$database = "MYMUSIC";
	$conn = mysql_connect("$location","$username","$password");
$year = (int)$_GET['year'];
if(!isset($_GET['year']))
{	
$year="1952";
}	
	if (!$conn) die ("Could not connect MySQL");
	mysql_select_db($database,$conn) or die ("Could not open database");

$query = "SELECT * FROM numberones  WHERE `year` = '$year'";
	$result = mysql_query($query);
	$numrows = mysql_num_rows($result);




echo "<table width='100%'  border='1' cellpadding='2' cellspacing='2' bordercolor='#006699' align='center'>"; 
echo "<tr>";
echo "<td width='100%' background='../phpbb2/templates/fisubsilversh/images/cellpic3.gif'><div align='center' class='style2 style3'><strong>
          $year </strong></div></td>";
echo "</tr>";
echo "</table>";
echo "<table width='100%'  border='1' cellpadding='2' cellspacing='2' bordercolor='#006699' align='center'>"; 




echo "<td width='13%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Date</div></td>";
echo "<td width='33%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Artist</div></td>";
echo "<td width='39%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Title</div></td>";
echo "<td width='6%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>Wks</div></td>";
echo "<td width='9%' background='../phpbb2/templates/fisubsilversh/images/cellpic1.gif'><div align='center' class='style1'>d/l</div></td>";	

	
	while($row = mysql_fetch_array($result))

 

{
		echo "<tr><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[year]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[artist]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[title]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[weeksatone]</div></td></tc>";
		echo "<tc><td background='../phpbb2/templates/fisubsilversh/images/cellpic_nav.gif'><div align='center'>$row[dllink]</div></td></tr>";
} 



echo "</table>"; 

?>
<?php include("music_footer.tpl") ?>
all you have to dois make links like:

Code: Select all

&amp;lt;a href="pagename.php?year=yearnumber"&amp;gt;Year Number&amp;lt;/a&amp;gt;
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

thank you very much i'm just gonna try it
as the links are on the same page (well an included page) will I need to refresh the page if so how or will it update automatic
thanks again
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

that works a treat I cant thank you enough and I don't have to refresh the page so thats cool too. :D
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

if I wanted to extract data from 2 fields what would the format be

Code: Select all

<a href="index1.php?year=1952,title=qwerty">Year Number</a>
and

Code: Select all

$query = "SELECT * FROM table where `year` = `$year` and `title` = `$title`";
and

Code: Select all

$year = (int)$_GET&#1111;'year']; 
if(!isset($_GET&#1111;'year']))
$title = (int)$_GET&#1111;'title']; 
if(!isset($_GET&#1111;'title']))
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<a href="index1.php?year=1952&title=qwerty">Year Number</a>

Code: Select all

$title = trim(addslashes($_GET&#1111;'title']));
$query = "SELECT * FROM table where `year` = '$year' and `title` = '$title'";
:)
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

thanx once again
on my url links when I click them can I load the page and set a variable at the same time I.E.

Code: Select all

&amp;lt;a href="http://127.0.0.1/phpBB2/music_site/index1.php","index1.php?header=1950s_header.tpl"&amp;gt;1950's&amp;lt;/a&amp;gt;
because i've realised I need to use a few different headers for different data that I display so I want instead of using

Code: Select all

<?php include("music_header.tpl") ?>
I want to use

Code: Select all

<?php include("$header") ?>
at the top of my page.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

yep, u can do that.

but the link has to look like this:

Code: Select all

&lt;a href="http://127.0.0.1/phpBB2/music_site/index1.php?header=1950s_header"&gt;1950's&lt;/a&gt;
and php code would be like:

Code: Select all

<?php
$header = $_GET['header'].".tbl";
require($header);
?>
this is not really safe, cos a user can include any file this way, i would load all valid header filesin a array and check to see if GET header is in the array or not, try that your self, see if you can do it, if not, you are welcome to come back and ask for help :).

functions you will need to checkout at php.net
[php_man]array[/php_man]
[php_man]in_array[/php_man]

good luck.
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

Thanks qads the master under your nick is truly deserved I'm working on that last bit now I'll let you know how I get on. :D
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

if I want to set 2 variables from a link what would be the format I tried this but it does'nt seem to work

Code: Select all

<a href="http://127.0.0.1/phpBB2/music_site/sgl_1950s.php?year=1960?navbar=music_navbar_1960s.tpl">1960's</a>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<a href="http://127.0.0.1/phpBB2/music_site/sgl_1950s.php?year=1960&navbar=music_navbar_1960s.tpl">1960's</a>
it is like this:

pagename.ext?I_want_GET&also_want_another_var&and_another&and_another
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

Thanks qads you have been a great help the more I use php the more I like it can you recomend any books on php for the novice user
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you welcome :).

i only got one book, php bible by joyce park, first edition, you may wanna get the 2nd edition though, its more update to date.
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

thanx i'll have a look for that this week
could you look at this and tell me where i'm going wrong I know i'm close

Code: Select all

<?php
if ($navbar = 5):
print_r($navbar);
include("music_navbar_1950s.tpl");
elseif ($navbar = 6):
print_r($navbar);
include("music_navbar_1960s.tpl");
endif;
?>
Im setting $navbar from a previous page when the page loads which holds the above code and $navbar = 5 the if statement executes correctly however if I give $navbar a 6 I can see that $navbar holds the value 6 but the include statement does'nt seem to execute
(edited) actually its not working at all it was another piece of code loading the navbar so any help would be great
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

$navbar seems to be holding the correct values so it must be the way i've formatted the include statement
Post Reply