?
Solved

Admin section for simple link script already made.

Posted on 2005-03-01
15
Medium Priority
?
251 Views
Last Modified: 2010-05-19
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
Comment
Question by:benwiggy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13433754
<?

$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
 
LVL 32

Expert Comment

by:Batalf
ID: 13433765
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
 
LVL 6

Author Comment

by:benwiggy
ID: 13433947
Thanks for your help - I also requested an admin section?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:Batalf
ID: 13434021
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
 
LVL 6

Author Comment

by:benwiggy
ID: 13434195
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13434226
There's nothing wrong with that line. Has to be something with the line above or below it.

Batalf
0
 
LVL 6

Author Comment

by:benwiggy
ID: 13434319
Sorry - your right - it all works - except the admin section needs to put an id in there auto incrementing...
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13434334
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
 
LVL 6

Author Comment

by:benwiggy
ID: 13434414
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13434451
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
 
LVL 6

Author Comment

by:benwiggy
ID: 13434461
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
 
LVL 6

Author Comment

by:benwiggy
ID: 13434470
Sorry - have moved <? - now I get a TSTRING error - will take a look...
0
 
LVL 32

Accepted Solution

by:
Batalf earned 1600 total points
ID: 13434477
<?

$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
 
LVL 6

Author Comment

by:benwiggy
ID: 13434502
Thanks - you have been fantastic and very patient - only fair I up the points a bit more!
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13434513
Thank you!

Always nice to help!

Thanks for the points and for the "A" grade

Batalf
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question