Link to home
Start Free TrialLog in
Avatar of benwiggy
benwiggyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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>'; }
  }
Avatar of Batalf
Batalf
Flag of United States of America image

<?

$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>";      
  }
 
 
 ?>
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.

Avatar of benwiggy

ASKER

Thanks for your help - I also requested an admin section?
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>

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.
There's nothing wrong with that line. Has to be something with the line above or below it.

Batalf
Sorry - your right - it all works - except the admin section needs to put an id in there auto incrementing...
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
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!
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>
I see: function deleteLink(inputID){ if(confirm('Click OK to delete this link')){ location.href='/elinksadmin.php?deleteID='+inputID; } } printed out on the screen...
Sorry - have moved <? - now I get a TSTRING error - will take a look...
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks - you have been fantastic and very patient - only fair I up the points a bit more!
Thank you!

Always nice to help!

Thanks for the points and for the "A" grade

Batalf