Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Admin section for simple link script already made.

Create an admin section for current script which will display links, lets you add them and lets your remove them - current script shown below.

In other words, instead of the $links array I would like the script to access a mysql database to retrieve the links specified by the elinks variable at the beginning of the script.

OR, if its easier - let the admin file access and change the text file containing the array.

 $extlinks = 1;
  $elinks = '1,3';

  if ($extlinks == 1) { echo 'Sponsored Links<BR>'; }
 
  $links = array(
    1 => 'Site1,URL1',
    2 => 'Site2,URL2',
    3 => 'Site3,URL3'
  );
 
  $a = explode(',', $elinks);
  $c = count($a);

  for ($n = 0; $n < $c; $n++) {
    $at = explode(',', $links[$a[$n]]);
    echo '<a href="',$at[1],'">',$at[0],'</a>'; if ($n < $c-1) { echo '<br>'; }
  }
0
benwiggy
Asked:
benwiggy
  • 8
  • 7
1 Solution
 
BatalfCommented:
<?

$extlinks = 1;
  $elinks = '1,3';

  if ($extlinks == 1) { echo 'Sponsored Links<BR>'; }
 
  $res = mysql_query("select ID,url,site from links where ID IN($elinks)");
  while($inf = mysql_fetch_array($res)){
      echo "<a href=\"".$inf["url"]."\">".$inf["site"]."</a><br>";      
  }
 
 
 ?>
0
 
BatalfCommented:
The solution assumes that you have a mysql table called links which is as follows

links
----------------------------
ID int
url varchar(255);
site varchar(255);

url = the url of the link
site = description of the link.

0
 
benwiggyAuthor Commented:
Thanks for your help - I also requested an admin section?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BatalfCommented:
Here's a small draft that could help you on your way to an admin section:

<?

$db = mysql_connect("host","username","password");
mysql_select_db("database_name",$db);

if($_POST['submit']){
      
      
      mysql_query("insert into links(url,site)values('".mysql_real_escape_string($_POST['url'])."','".mysql_real_escape_string($_POST['site'])."')");
      
      
}

?>
<body>
<?
echo "<table border=1>";
$res = mysql_query("select * from links");
while($inf = mysql_fetch_array($res)){
      echo "<tr><td><p>".$inf["url"]."</td><td><p>".$inf["site"]."</td></tr>";
}
echo "</table>";
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="Post">
<table border="0">
      <tr><td>Url:</td><td><input type="text" name="url"></td></tr>
      <tr><td>Site:</td><td><input type="text" name="site"></td></tr>
      <tr><td colspan="2"><input type="submit" name="submit" value="Save"></td></tr>
</table>
</form>
</body>
</html>

0
 
benwiggyAuthor Commented:
When your script creates each link the id is null.

Also, I get an error: unexpected T_VARIABLE for this line:
$res = mysql_query("select ID,url,site from links where ID IN($elinks)");

Thanks.
0
 
BatalfCommented:
There's nothing wrong with that line. Has to be something with the line above or below it.

Batalf
0
 
benwiggyAuthor Commented:
Sorry - your right - it all works - except the admin section needs to put an id in there auto incrementing...
0
 
BatalfCommented:
Yes, the table should be created like this:

create table links(
ID int auto_increment not null primary key,
url varchar(255),
site varchar(255));

Then the admin code above should work.

Batalf
0
 
benwiggyAuthor Commented:
Finally could you add an option for removing links to the admin section,
P.S. For exceeding what I asked I have incremented the points to 350 - Thanks!
0
 
BatalfCommented:
Try this:

<?

$db = mysql_connect("host","username","password");
mysql_select_db("database_name",$db);

if($_GET['deleteID']){
      mysql_query("delete from links where ID='".$_GET['deleteID']."'");      
}

if($_POST['submit']){
     
     
     mysql_query("insert into links(url,site)values('".mysql_real_escape_string($_POST['url'])."','".mysql_real_escape_string($_POST['site'])."')");
     
     
}

?>
<body>
<script type="text/javascript">
function deleteLink(inputID){
      if(confirm('Click OK to delete this link')){
            location.href='<? echo $_SERVER['PHP_SELF']; ?>?deleteID='+inputID;      
      }
}
<?
echo "<table border=1>";
$res = mysql_query("select * from links");
while($inf = mysql_fetch_array($res)){
     echo "<tr><td><p>".$inf["url"]."</td><td><p>".$inf["site"]."</td><td><a href=\"#\" onclick=\"deleteLink(".$inf['ID'].")\">Delete</a></td></tr>";
}
echo "</table>";
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="Post">
<table border="0">
     <tr><td>Url:</td><td><input type="text" name="url"></td></tr>
     <tr><td>Site:</td><td><input type="text" name="site"></td></tr>
     <tr><td colspan="2"><input type="submit" name="submit" value="Save"></td></tr>
</table>
</form>
</body>
</html>
0
 
benwiggyAuthor Commented:
I see: function deleteLink(inputID){ if(confirm('Click OK to delete this link')){ location.href='/elinksadmin.php?deleteID='+inputID; } } printed out on the screen...
0
 
benwiggyAuthor Commented:
Sorry - have moved <? - now I get a TSTRING error - will take a look...
0
 
BatalfCommented:
<?

$db = mysql_connect("host","username","password");
mysql_select_db("database_name",$db);

if($_GET['deleteID']){
      mysql_query("delete from links where ID='".$_GET['deleteID']."'");      
}

if($_POST['submit']){
     
     
     mysql_query("insert into links(url,site)values('".mysql_real_escape_string($_POST['url'])."','".mysql_real_escape_string($_POST['site'])."')");
     
     
}

?>
<body>
<script type="text/javascript">
function deleteLink(inputID){
      if(confirm('Click OK to delete this link')){
            location.href='<? echo $_SERVER['PHP_SELF']; ?>?deleteID='+inputID;      
      }
}
</script>
<?
echo "<table border=1>";
$res = mysql_query("select * from links");
while($inf = mysql_fetch_array($res)){
     echo "<tr><td><p>".$inf["url"]."</td><td><p>".$inf["site"]."</td><td><a href=\"#\" onclick=\"deleteLink(".$inf['ID'].")\">Delete</a></td></tr>";
}
echo "</table>";
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="Post">
<table border="0">
     <tr><td>Url:</td><td><input type="text" name="url"></td></tr>
     <tr><td>Site:</td><td><input type="text" name="site"></td></tr>
     <tr><td colspan="2"><input type="submit" name="submit" value="Save"></td></tr>
</table>
</form>
</body>
</html>
0
 
benwiggyAuthor Commented:
Thanks - you have been fantastic and very patient - only fair I up the points a bit more!
0
 
BatalfCommented:
Thank you!

Always nice to help!

Thanks for the points and for the "A" grade

Batalf
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now