www.webdeveloper.com
Results 1 to 13 of 13

Thread: Join values

  1. #1
    Join Date
    May 2007
    Posts
    77

    Join values

    Hi,

    I have one file search_result_template.php

    Code:
    <?
    	$displaysearch 	= htmlspecialchars(stripslashes($_GET["q"]));
    
    	//inserting data order in database
    	
    	$select=mysql_query("SELECT * FROM v2tags WHERE tag='$displaysearch' LIMIT 1") or die(mysql_error()); 
    if( mysql_num_rows($select) ){ 
        mysql_query("UPDATE v2tags SET cnt=cnt+1 WHERE tag='$displaysearch'") or die(mysql_error()); 
    }else{ 
        mysql_query("INSERT INTO v2tags SET tag='$displaysearch'") or die(mysql_error()); 
    	
    } 
    for($i=0;$i<count($info['results']);$i++)
    {
    	print
    	'<tr>
    		<td class="item">
    			<img src="./images/'.$info['results'][$i]['icon'].'" width="48" height="48" border="0" alt="'.$info['results'][$i]['icon'].'" />
    		</td>
    		<td class="item">
    			<table>
    				<tr><td>'.$info['results'][$i]['title'].'</a>'.$info['results'][$i][''].' '.$info['results'][$i]['source'].'</td></tr>
    				<tr><td>
    					<a target="_self" href="download.php?file='.$info['results'][$i]['id'].'&desc='.$info['results'][$i]['title'].'">'.$info['results'][$i]['url'].'</a>
    				</td></tr>
    			</table>
    		</td>
    	</tr>
    	'."\n";
    }
    ?>
    And one download.php

    Code:
    <? 
    $file = $_GET['file'];
    echo "$file";
    $desc = $_GET['desc'];
    echo "$desc";
     ?>
    This give me a url like this http://www.site.....com/download.php...238&desc=Name1 Name2...

    This script download.php is working but only show one value.

    I need in download.php to show all related files with Name1 and Name2

    Is possible make this?

    Thank you for your help

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Issue #1

    Quote Originally Posted by RAN View Post
    This give me a url like this http://www.site.....com/download.php...238&desc=Name1 Name2...
    Nope, it doesn't.

    search_result_template.php generates unique links, each in their own table cell, each with only these arguments:

    ?file=nnnnnn&descr=Namen (where "n" in lower case is a number)

    So your download script as written works with the URL that's generated when a user clicks on any link.

    Issue #2

    If you want to pass multiple instances in a URL, here are 3 ways of many more possibilities to do it:

    1) Just add an integer on the end of the argument name

    ?filen=nnnnnn&descrn=Namen&filen=nnnnnn&descrn=Namen...

    2) Use a separator character such as | (pipe) or something between the values using only two arguments:

    ?file=nnnnnn|nnnnnn...&descr=Namen|Namen...

    3) Create an array for files and description, serialize it and pass it to the URL (you could even use JSON for this):

    ?file=hash&descr=hash (where hash is a serialized array)

    Then in download.php, depending on which method you choose above, parse the URL accordingly to extract the files and descriptions.

    Hope this gets you thinking about some cool possibilities and on the right track to coding a cool solution. Others might chime in with more methods or code as they see fit, of course. These are just suggestions based on what I think you're trying to do.

    -jim

  3. #3
    Join Date
    May 2007
    Posts
    77
    Hi,

    Is to much from me, but tank you for your help.

    And if I have an url like this:
    http://www.site.....com/download.php...238&desc=name1 name2...

    in download.php I have a GET Values From URL and join related values?

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Knowing your code doesn't generate that, it must be pointed out the example URLs you've posted is not advised. You seem to be using a space as a separator. But in the description what if a space exists already?

    Pretending you adjusted the first script to generate an actual URL using a space as a deliminator between each file ID and description as you insist, download.php could be coded similar to this:

    PHP Code:
    $file_arr=explode(" ",$_GET['file']);
    $description_arr=explode(" ",$_GET['desc']);

    for(
    $counter=0$counter<count($file_arr);$counter++) {

       print 
    "<br />File $counter = $file_arr[$counter]";
       print 
    "<br />Description $counter = $description_arr[$counter]<br />";


    One approach of many you could take, highly simplified for proof of concept only. Just use the PHP explode command to get each file and description in its own array so you can loop through them to display, do whatever you want. Remember what I said though about the space. That's why I suggested the 3 alternatives above, no worries like that.

    -jim

  5. #5
    Join Date
    May 2007
    Posts
    77
    Ok,
    Thank you,

    Now I have in download page this results:

    File 0 = 1018923
    Description 0 = Name1

    How can I go to database search for all values "Name1" and print this values in download page like this:

    Mabe a text area with related links like this:

    Name1 --> mouse over ---> link file1.rar
    Name1 --> mouse over ---> link file2.rar
    Name1 --> mouse over ---> link file3.rar
    Name1 --> mouse over ---> link file4.rar

    and so one

    "Pretending you adjusted the first script to generate an actual URL using a space as a deliminator between each file ID and description as you insist" I did't insist, I don't know how to make in other way...


    Can you help me please?

    Thank you
    Last edited by RAN; 02-17-2010 at 11:12 AM.

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    What do you mean how? Are you familiar with PHP and MySQL and how to do queries, work with a result set and echo via PHP? If you are, please post code and we will assist you.

  7. #7
    Join Date
    May 2007
    Posts
    77
    Ok,

    Now I have this in url:

    http://www.site.....com/download.php?file=943498

    With this id in url I can go to database and show the values of id 943498

    I put in download.php

    Code:
    <?
    
    
    $dbhost="localhost";
    $username="cpanel";
    $password="cpanel";
    $database="cpanel";
    
    $id = $_GET['file'];
    
    mysql_connect(localhost,$username,$password);
    mysql_select_db($database) or die( "Unable to select database");
    
    $query="SELECT * from v2links WHERE id=$id";
    
    $result=mysql_query($query);
    
    //get the number of rows in your array for loop
    $num=mysql_numrows($result);
    
    mysql_close();
    
    //Here's the loop that uses $num from above as a boundary. The loop will seperate each data into a unique variable then echos out each however you like before it loops back.
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $caption=mysql_result($result,$i,"caption");
    $url=mysql_result($result,$i,"url");
    
    echo "$id - $caption - $url";
    
    $i++;
    } 
     ?>
    Now the results in download page are 943498 - Hamburg Music Concert -file1.rar

    Is possible make a looop to search in database and show all "Hamburg Music Concert" related items and print them?

    Thank you

  8. #8
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    PHP Code:
    $query="SELECT * from v2links WHERE id=$id"
    If you want to search for all files with the phrase you specified in the description, based on the code and information you've provided so far, you could modify the query like this:

    select * from vslinks where caption like "&#37;Hamburg Music Concert%"

    The above example assumes you have a search tool or some device where the user can specify what criteria to search for. I don't know your database structure other than in the code you provided so adjust the query field name to suit your needs as necessary (i.e. "caption"). All I did was use the MySQL "like" command which searches for any instance in the specified field of the chars between the %% delimiters unlike the use of "where" which is used for an exact match as you did correctly for the ID search in your original query.

    Have fun!

    -jim
    Last edited by SrWebDeveloper; 02-17-2010 at 01:48 PM. Reason: clarification

  9. #9
    Join Date
    Nov 2008
    Posts
    2,477
    You should also read up on SQL injection.

  10. #10
    Join Date
    May 2007
    Posts
    77
    Ok,

    I have a search result:

    http://www.site.....com/download.php?file=943498

    In download results he only show one result of "Hamburg Music Concert" but in database I have "Hamburg Music Concert" link1 "Hamburg Music Concert" link2 "Hamburg Music Concert" link3 and so one.

    I need instead of download.php show only one result, show all results "Hamburg Music Concert"

    Like this:

    RELATED FILES:

    Hamburg Music Concert--->mouse over--->link1
    Hamburg Music Concert--->mouse over--->link2
    Hamburg Music Concert--->mouse over--->link3

    I have another search result
    http://www.site.....com/download.php?file=1111111

    In download results he only show one result of "Bob Dylan" but in database I have "Bob Dylan" link1 "Bob Dylan" link2 "Bob Dylan" link3 and so one.


    I need instead of download.php show only one result, show all results "Bob Dylan"

    Like this:

    RELATED FILES:

    Bob Dylan--->mouse over--->link1
    Bob Dylan--->mouse over--->link2
    Bob Dylan--->mouse over--->link3


    And so one.

    Thank you

  11. #11
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    The query I gave you (aside from field name changes as I noted) using the like command in MySQL will do that if more than one row exists. I looked at your code which loops through the result set in reply #7 and it will return more than one row from what I can tell.

    Did you try my code and make adjustments?

    Post the new code showing the query and also post the relevant code that loops through the result set and echos. From what I can tell, your questions was asked and answered, but there may be a language barrier going on here. Thanks for your effort so far.

    -jim
    Last edited by SrWebDeveloper; 02-17-2010 at 02:58 PM. Reason: confusion

  12. #12
    Join Date
    May 2007
    Posts
    77
    Thank you so much for your help

    My database table "v2links" database colums "id, url, caption"
    "url is the link like http://www.site.com/page.php" and "caption is the title like Bob Dylan"

    I already try this:

    Code:
    <?
    
    include 'config.php';
    
    $id = $_GET['file'];
    
    mysql_connect(localhost,$dbuser,$dbpass);
    mysql_select_db($dbname) or die( "Unable to select database");
    
    $query="SELECT * from v2links WHERE caption like "&#37;id%"
    
    
    $result=mysql_query($query);
    
    //get the number of rows in your array for loop
    $num=mysql_numrows($result);
    
    mysql_close();
    
    //Here's the loop that uses $num from above as a boundary. The loop will seperate each data into a unique variable then echos out each however you like before it loops back.
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $caption=mysql_result($result,$i,"caption");
    $url=mysql_result($result,$i,"url");
    
    echo "$id - $caption - $url";
    
    $i++;
    } 
    
    ?>
    But give me an error
    Parse error: syntax error, unexpected T_STRING in /home/site/public_html/site.com/download.php on line 142

    Line 142 is $id=mysql_result($result,$i,"id");

    I try to replace "%id%" with "%caption%" but gives me the same error.

    Thank you

  13. #13
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Change query to:

    PHP Code:
    $query="SELECT * from v2links WHERE caption like '%$id%'" 
    That will fix the error and pass the proper variable. You inserted "id" not the variable named $id and you included a double quote without escaping it (I opted to wrap %$id% with a single quote).

    Now, listen carefully, about this ID thing...

    In your code $id is populated from $_GET['file'] which is populated by whatever the argument named "file" is in the URL for that page. You asked how to find all rows based on partial matches of the caption. If the "file" argument is indeed the caption text the user is searching for, fine. But I strongly suspect file contains the ID based on your code and what you wrote all along. If that is true, refer back to when I said "The above example assumes you have a search tool or some device where the user can specify what criteria to search for." I was referring to the caption, i.e. where does "Hamburg Music Concert" search criteria come from? If the query returns the wrong data, now you know why - in advance.

    On a side note, once you knock this out, follow Mindzai's excellent advice and learn more about PHP/MySQL SQL injection security protection - your script is very vulnerable. This you can do on your own, view tutorial here.

    -jim
    Last edited by SrWebDeveloper; 02-17-2010 at 07:35 PM. Reason: found a nice tutorial about sql injection so added that in

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles