Solved

Edit Link Created By SQL Query

Posted on 2013-12-19
21
319 Views
Last Modified: 2014-01-03
Aloha,

I am being asked to change a script created URL to a custom URL. We have a php script that builds our navigation menu through a SQL call. Here is that script:

require_once('includes/config.inc');
$htmlnav ='';
$htmlnav.='<div id="left">
    	<div id="nav">
		<ul id="first">';
		
$sql = "SELECT * FROM tbl_main_links ORDER BY fld_main_link_id";
  #### Query 1 MAIN LINKS ####
                    $sql = "SELECT * 
                    		FROM tbl_main_links ml
                    		WHERE ml.fld_status ='1'
							ORDER BY ml.fld_sort ";
					$result = mysql_query($sql, $db_connection) or die(mysql_error());
					$catArray = array(); 
                    $name     = array();
                    ## fetch an array ##
                    while ($row = mysql_fetch_assoc($result)):
                    
                    ## build the arrays for looping through sub links ##
                    $catArray[] = $row['fld_main_link_id'];
                    $text[]     = $row['fld_text'];
                    $ssL[]      = $row['fld_ss'];
                    $link[]     = 'index.php?id='.$row['fld_main_link_id'].'&ss='.$row['fld_ss'].'&'.$row['fld_link'];//$row['fld_link']
					

					## turn on active link style Michele added this code ##				
					$linkon = $row['fld_link'];				
					$active = explode('=', $linkon);
					$activelink = $active[2];
						if ($activelink == $header)
						{
						$on[] = 'on';
						} else {
						$on[] = ' ';
						}
						
					endwhile;
                     
                    ## Loop through the MIAN LINK PRIMARY KEYS ##
                    foreach ($catArray as $key => $value)
					{
					  #### Show MAIN LINK TEXT and build the LINKS with them ####
					$htmlnav.=' <li><a href="'.$link[$key].'" class="'.$on[$key].'">'.$text[$key].'</a></li>';
					 if($_GET['id']==$value){
					  #### Query 2 Rates ####
					 $sql2 = "	SELECT * 
					 			FROM tbl_sub_links t
							 	WHERE t.fld_parent_id = '$value'
							 	AND t.fld_status ='1'
								ORDER BY t.fld_sub_link_id ";
					$result2 = mysql_query($sql2, $db_connection) or die(mysql_error()); 
					$totalRows = (mysql_num_rows($result2));
					####### ONLY SHOW THE <ul id="second"> FOR IYTEMS WITH SUBLINKS #############						
					    if($totalRows >=1)
						{
                        $htmlnav.='<ul id="second">';
                        }
                       #### Loop through the SUB LINKS and MAKE LINKS FROM them ####
                     while ($row2 = mysql_fetch_assoc($result2)):
                    $htmlnav.='<li><a href="index.php?&id='.$row2['fld_parent_id'].'&'.$row2['fld_link'].'&ss='.$ssL[$key].'">'.$row2['fld_text'].'</a></li>';
                    endwhile;
                    ### ONLY CLOSE IT IF WE NEEDED IT ######
                    if($totalRows >=1)
						{
                    	$htmlnav.=' </ul>';
                    	}
                    }##END the condition
                    ## End the loop through the NAV LINKS ###
             	}
             	$htmlnav.='
             	</ul>
        <img src="images/nav_bottom.jpg" />
		</div>
		<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="200" height="800">
 		<param name="movie" value="images/MOC.swf" /> 
		<param name="quality" value="high" /> 
		<param name="bgcolor" value="#0C7CB0" /> 
		<embed src="images/MOC.swf" quality="high" bgcolor="#0C7CB0"  width="200" height="800" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
		</object>
		</div>';

Open in new window


It outputs a URL list that looks like this:

http://mysite.com/index.php?id=1&ss=0&page=home&content=home
http://mysite.com/index.php?id=2&ss=0&page=visitor&content=visitor
http://mysite.com/index.php?id=3&ss=0&page=tickets&content=tickets
http://mysite.com/index.php?id=4&ss=0&page=shop&content=shop
http://mysite.com/index.php?id=5&ss=0&page=dine&content=dine

I want to replace the output of (for example) id 4 and change the URL to just something like 'http://www.someothersite.com'

Being a PHP newbie, I am not sure where to start. Any direction would be greatly appreciated.

This script is being called by index.php when the site is loaded, if that helps.

Mahalo in advance,

Tony

ed. note: Put the code into the code snippet ~Ray
0
Comment
Question by:tlasoya808
  • 10
  • 6
  • 5
21 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I think it actually outputs "index.php?id=1&ss=0&page=home&content=home".  To substitute a different link, you need to detect the 'id' you want to replace and replace the

$htmlnav.='<li><a href="index.php?&id='.$row2['fld_parent_id'].'&'.$row2['fld_link'].'&ss='.$ssL[$key].'">'.$row2['fld_text'].'</a></li>';

statement with the URL you want to use.
0
 

Author Comment

by:tlasoya808
Comment Utility
You are correct. But it inserts http://www.mysite.com in front of the index.php? url.

I just want a simple link to http://somesite.com.

Is there a statement that I can add to the end of the loop that does:

If id=4 then replace url for id4 with http://somesite.com

Which is what I think you are stating. Just not sure where to put it.

For example:

If ['ml.fld_sort'] = 4
 $htmlnav='<li><a href="http://mysite.com></a></l>

I tried that, placing it in front of:

$htmlnav.='
                   </ul>

And it didn't work.

Thanks for your assistance!
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
The browser inserts the domain when it requests the page.  I think you will see that the domain is not listed when you view the source of the web page.

 $htmlnav='<li><a href="http://mysite.com></a></l> is missing the last 'i' and the dot '.' after "$htmlnav.=".  And in the code you posted above, $row2['fld_parent_id'] is the id you should be looking for.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think what you want is a "fully qualified URL," including the http://www... part.  Have a look at line 23 in the snippet above:

$link[] = 'index.php?id='.$row['fld_main_link_id'].'&ss='.$row['fld_ss'].'&'.$row['fld_link'];//$row['fld_link']

Probably that should be something like this:

$link[] = 'http://www.someothersite.com/index.php?id=...
0
 

Author Comment

by:tlasoya808
Comment Utility
Right :)

Well, the corrected code:

If ['fld_parent_id'] = 4
 $htmlnav.='<li><a href="http://mysite.com></a></li>


Throws this error condition:

Parse error: syntax error, unexpected '[', expecting '(' in /home/moc/public_html/nav.php on line 71

Line 71 is the first line of code.

Google is little help (always go there first :) Ideas?

Thanks!
0
 

Author Comment

by:tlasoya808
Comment Utility
I see what you are saying, Ray. But this would be just for one of the options, not all of them. I'd like to just do away with SQL script (the urls never change anymore) and just hard code the links.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
It should be...

if($row2['fld_parent_id'] == 4)

When you get this working and you want to hard code them, you can just put them in an array and 'foreach' thru them.
0
 

Author Comment

by:tlasoya808
Comment Utility
Thank you. It didn't throw an error this time :)

Now, as to placement. I put it where I referenced before (prior to line 70 in the above example). It had no effect.

Ideas on where to place it?

(this whole experience is telling me I need to get a handle on php :)
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
(this whole experience is telling me I need to get a handle on php :)
Yes.  Read Ray's article... http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Starting at line 59 above...
while ($row2 = mysql_fetch_assoc($result2)):
if($row2['fld_parent_id'] == 4) {  $htmlnav.='<li><a href="http://mysite.com>MySite</a></li>';}
else { $htmlnav.='<li><a href="index.php?&id='.$row2['fld_parent_id'].'&'.$row2['fld_link'].'&ss='.$ssL[$key].'">'.$row2['fld_text'].'</a></li>';}
endwhile;
 

Open in new window

0
 

Author Comment

by:tlasoya808
Comment Utility
Thanks for that link. That will be my next step once this is working.

I put the code where you suggested and it had no effect. I am wondering if something shouldn't go in where the main links are built starting at line 40? Where we are looking is where the sublinks go.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Not if it is creating these links:

http://mysite.com/index.php?id=1&ss=0&page=home&content=home

There is only one line that creates those links... actually, I can't find any line in your code that could create those links.  None of them has all the variables, id, ss, page, and content.
0
 

Author Comment

by:tlasoya808
Comment Utility
That url shows up in line 23. Will play around where I *think* it may go...
0
 

Author Comment

by:tlasoya808
Comment Utility
Do you think this PHP code in index.php could be causing some grief?

<? 
$header =(isset($_GET['page']) AND ($_GET['page']!=''))? $_GET['page']: 'home';
$content=(isset($_GET['content']) AND ($_GET['content']!=''))? $_GET['content']: 'home';
$_GET['ss']=(isset($_GET['ss']) AND ($_GET['ss']!='') AND (is_numeric($_GET['ss'])) )? $_GET['ss']: '0';
include('nav.php');
$ID = (isset($_GET['id']) AND ($_GET['id']!='')  AND (is_numeric($_GET['id'])) )? $_GET['id']: '1';
$sql = "SELECT fld_title, fld_keywords, fld_description 
        FROM tbl_main_links 
        WHERE fld_main_link_id = $ID";
		$result = mysql_query($sql, $db_connection) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
The problem with starting from a code base that does not work is pretty simple -- unworking code is less than useless, it is a distraction from what we want to achieve.  What we really need to see is the input you have and the output you want to create.  Armed with that information we can usually help you bridge the gap.

However in this instance it is difficult to see what the input contains.  Here is what I suggest, based on the most recent code snippet.

Change the query to add the LIMIT 1 clause (since you're only using one row from the results set).  This will improve performance.

Print out the contents of the $row variable with var_dump($row).

Then based on the contents of the information in the results set, tell us what you want to get into the HTML document.

Thanks, ~Ray
0
 

Author Comment

by:tlasoya808
Comment Utility
Aloha Ray,

I will attempt to do what you suggested.

For the record, I inherited this site, written back in the late 90's. I was (in a previous life) a network engineer, so there is SOME logic in this brain.

Thanks for hanging in there with me.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
late 90's
That's about when I started writing PHP, in the PHP3 days.  It's changed a lot since then, and most of the code I wrote a decade or more ago needs to be refactored.  I'm sure this needs refactoring, too.
0
 

Author Comment

by:tlasoya808
Comment Utility
Agreed! And thanks for the link to the book (and for Dave's PHP link). I am going to call it a night and take this back up tomorrow. If I don't get anywhere (and it looks like refactoring, or just a new site) is the answer, I will split the points between the two of you.

Aloha.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
That link is an article written by Ray...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I actually have enough points to orbit Saturn, but thanks for your kind words :-)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Happy New Year 2014, and thanks for using EE, ~Ray
0
 

Author Comment

by:tlasoya808
Comment Utility
Same to you, Ray. Sorry for the delay in closing out the question.

Aloha!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to count occurrences of each item in an array.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now