Jump to content


Switch on/off?


10 replies to this topic

#1 jold101

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Somerset,UK
  • Interests:Creating websites with (x)HTML coding, PHP coding, CSS styling, AJAX and MySQL databases. Online gaming (Counter Strike etc.) and golf.

Posted 31 March 2007 - 09:31 AM

Hey P2L community.

I need a bit of code, I haven't learnt much PHP yet.

I have a news script, it is pretty simple and uses MySQL backend. Now I would like to have a Turn On/Off function in the Admin Control Panel. I suppose I would have to create a table for it in PHPMyAdmin.

Let me explain it a bit more...

If in the admin control panel the News is set to "1" (or "on"), the news on index.php would display the news.
But, if it is set to "0" (or "off"), the index.php would display:

News has been disabled.

Here is the code for the news:

<?php
include ('mysql_connect.php');
$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC LIMIT 4";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$url = 'comments.php?id='.$row['id'];
$row['id'] = array_reverse($row);
echo '<p><b>'.$row['title'].'</b><br />
'.$row['sd'].'<br />
Posted by : <b>'.$row['author'].'</b><br />
'.$row['post'].'<br />';
}

} else {
echo 'There are no news posts to display';
}
?>

Thanks.

Edited by jold101, 31 March 2007 - 09:32 AM.


#2 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 10:11 AM

the way i would do it is make news a module file

so put:

<?php
include ('mysql_connect.php');
$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC LIMIT 4";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$url = 'comments.php?id='.$row['id'];
$row['id'] = array_reverse($row);
echo '<p><b>'.$row['title'].'</b><br />
'.$row['sd'].'<br />
Posted by : <b>'.$row['author'].'</b><br />
'.$row['post'].'<br />';
}
}
?>

inside a file called news.php

then in phpmyadmin

create a new table called modules

with id, file, status, name

then add the news module into the database

file set as location to news.php

name set as news

status set as on

then create another file

this time called newscontainer.php

in that put

your database query, but this time looking at the modules table and news entry

then put

if($rows['status'] == 'on'){
include('$row['file']');
} else {
echo" news disabled ";
}


then include newscontainer.php where you want news to show on the index.php

something along those lines anyway, try it out....if we tell u everything, you would never learn

Edited by curthard89, 31 March 2007 - 10:12 AM.


#3 jold101

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Somerset,UK
  • Interests:Creating websites with (x)HTML coding, PHP coding, CSS styling, AJAX and MySQL databases. Online gaming (Counter Strike etc.) and golf.

Posted 31 March 2007 - 10:29 AM

Thanks for your reply, it took me a white to work out, but I have got it now. I'll check see if it works.

#4 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 10:48 AM

yer kool, let me know if it works, was off top of my head

#5 jold101

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Somerset,UK
  • Interests:Creating websites with (x)HTML coding, PHP coding, CSS styling, AJAX and MySQL databases. Online gaming (Counter Strike etc.) and golf.

Posted 31 March 2007 - 10:55 AM

I done all of what you said. I looked at newcontainer.php and got this:

Parse error: syntax error, unexpected T_STRING in C:\localweb\htdocs\newscontainer.php on line 3.

#6 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 11:11 AM

lol, the way i described wasnt really in any order at all, just something you will have to play around with, making sure you have ended if's and other things, the module system i use on my cms is rather complex and has about 14 files that all link togther to print out modules on a page.

sorry i cant help you anymore......i havnt really got time to sit down and write loads on it. As i do alot of work for companies i have to get on with there stuff, play around with it, try your hardest, if the worst comes to the worst, i will have to write the full code.

#7 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 11:14 AM

can you give me the code for newscontainer.php

#8 jold101

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Somerset,UK
  • Interests:Creating websites with (x)HTML coding, PHP coding, CSS styling, AJAX and MySQL databases. Online gaming (Counter Strike etc.) and golf.

Posted 31 March 2007 - 11:33 AM

<?php
if($rows['status'] == '1'){
include ('$row['file']');
} else {
echo" News Disabled ";
}
?>

Edited by jold101, 31 March 2007 - 11:33 AM.


#9 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 11:38 AM

you need to put

if($rows['status'] == '1'){
include ('$row['file']');
} else {
echo" News Disabled ";
}

withing a sql query,

so you query the module database

so instead of echo " results";

you put

if($rows['status'] == '1'){
include ('$row['file']');
} else {
echo" News Disabled ";
}

that :)

#10 jold101

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Somerset,UK
  • Interests:Creating websites with (x)HTML coding, PHP coding, CSS styling, AJAX and MySQL databases. Online gaming (Counter Strike etc.) and golf.

Posted 31 March 2007 - 11:52 AM

The code I used and the code you just gave me are identical.

#11 curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 31 March 2007 - 12:24 PM

lol, no, its howi worded it sorry,

basically the code is fine, u just need to put it within the "while" statement of the sql query which draws information from the modules table

Edited by curthard89, 31 March 2007 - 12:32 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users