Help with this function


#1

I don’t understand what is wrong in this function

Function Getlink ($Getlink){
$newlink="http:www.prova.it/pagina.php?id=";
$getlink=array(
array("$newlink", "1234"),
array("$newlink", "6353"),
array("$newlink", "8372"),
array("$newlink", "3490"),
array("$newlink", "5739") 
);
echo $getlink [0][0] . $getlink [0][1]."<br>";
echo $getlink [1][0] . $getlink [1][1]."<br>" ;
echo $getlink [2][0] . $getlink [2][1]."<br>";
echo $getlink [3][0] . $getlink [3][1]."<br>";
echo $getlink [4][0] . $getlink [4][1]."<br>";

}

It print for every line all the arrays while I would like to see in the first line just the first , then in the second line the second etc

here’s what it prints

http:www.prova.it/pagina.php?id=1234
http:www.prova.it/pagina.php?id=6353
http:www.prova.it/pagina.php?id=8372
http:www.prova.it/pagina.php?id=3490
http:www.prova.it/pagina.php?id=5739

http:www.prova.it/pagina.php?id=1234
http:www.prova.it/pagina.php?id=6353
http:www.prova.it/pagina.php?id=8372
http:www.prova.it/pagina.php?id=3490
http:www.prova.it/pagina.php?id=5739

http:www.prova.it/pagina.php?id=1234
http:www.prova.it/pagina.php?id=6353
http:www.prova.it/pagina.php?id=8372
http:www.prova.it/pagina.php?id=3490
http:www.prova.it/pagina.php?id=5739

Here’s how I would like to see it

http:www.prova.it/pagina.php?id=1234

http:www.prova.it/pagina.php?id=6353

http:www.prova.it/pagina.php?id=8372

http:www.prova.it/pagina.php?id=3490

http:www.prova.it/pagina.php?id=5739

Thanks


#2

Try this:

<?PHP

$newlink="http://www.prova.it/pagina.php?id=";
$getlink=array( 1234, 6353, 8372, 3490, 5739 );
foreach ($getlink as $link) {
    echo $newlink . $link . "<br>";
}

?>

#3

Thank you! i tried in different ways but everytime there was an error! I’m just hoping in time it will be more natural to use php instead of taking a lot of time on easy programs


#4

Arrays are either indexed or not indexed. Meaning you can do them like the current one which is not indexed.
An indexed one would be like this:
$array_name = array (
“car1” => “Chevy”,
“car2” => “Ford”,
“car3” => “Porsche” );
Then, you would access it with $array_name(“car2”) to get the Ford out…

So, then there are multi-dimensional ones which can contain indexed or not indexed arrays stacked or
nested as deep as you need them. Here is a good site for starting off learning about parts of PHP.
Their code is not always the best way, but, they explains a lot of things and is good for starting out…

WWW Schools Arrays


#5

It still print me for every line all the array!!image

What is wrong?


#6

image|374x500


#7

That function prints everything that was setup in it. You are saying that it prints it multiple times. So, you are calling that function multiple times for it to happen.

Rather than tell you how to fix this, what are you actually trying to solve with this? Or is it simply that you don’t understand what is happening?

function Getlink takes in an argument of $Getlink, which isn’t used. Why is it there to begin with?
You are calling the function multiple times for hard coded values. Why?


#8

With that function I am trying to put those links in a new column where every name has is own link


#9

Did you try my code I posted? It works.
Otherwise, show your current code. You do not want a function to call itself just to loop thru an array.
Show some code…


#10

Please post a sample of where you are calling this Getlink function. There is clearly something wrong here, ie the $Getlink param is never actually used in the function.

I assume you somewhere iterate (loop) over a few links you want to display and for each link you run the Getlink function. But if that function has 5 echoes in it then you WILL get 5 lines each time you call it.

So please post the code where you call this function. and preferably the HTML output of at least 1 of the iterations (where you now get too many links) with a modified output reflecting what you actually want it to look like


#11

i resolved this problem it was easier than i tought

function getLink ($getlink){
$newlink=“http:www.prova.it/pagina.php?id=”;
return $newlink . $getlink ;
}


#12

my doubt is …in this exercise i’ve to make a table html in a a new page i open through the links used before, how can i do that? sorry if i post just part of this exercise but i’m italian and the exercise is in italian too


#13

Honestly, that is an odd thing to do. If all you are doing is appending an id to a URL, a function really isn’t needed.


#14

Yeah, what he said. Funny LOL is even the function itself is overcoded and it is only 3 lines. You could have just done this…(Not that you should)

function getLink ($getlink){
return "http:www.prova.it/pagina.php?id=$getlink";
}

#15

with that function, for every doctor i have a link using their id to open a new page , now that link must read that id and search in the array (containg all the infos) which is the doctor and find all the info about him/her and than printing them in the single links… do you suggest to use the function array_search?


#16

The code you have been posting isn’t building html links, it is just building the URL part of a link, which are missing the // in them as well, so, nothing up to this point is complete.

The correct syntax for a html link is - <a href='a valid URL'>some text, image, or other content</a>

If you are doing this in order to learn how to build web applications, any link you produce on a page should include some or all of any existing get parameters from the url for that page. If you reached the page you are producing these links on after selecting some search, filter, or pagination values, you would want the links you are producing to include those existing values. The way to do this is to get a copy of the current get parameters, add the id parameter, then build the link with the resultant set of get parameters. You would want to use php’s http_build_query() function to build the query string part of the URL since it url encodes the data and builds a correct query string for any number of parameters that you supply.

An example of doing this would be -

$get = $_GET; // get a copy of any existing get parameters
$get['id'] = some_id_value; // add the id parameter and value.
$qs = http_build_query($get,'', '&amp;');
$link = "<a href='http://your_domain/your_file.php?$qs'>some text, image, or other content</a>";
// LOL this ridiculous forum software is even altering the display of code, it removed ' text' from the above resulting in someimage instead of some text, image, or other content
// use the resulting $link variable

There are three ‘dynamic’ values in the above that you need to supply - 1) the id value, the base URL (protocol, domain, and file), and the text/image/content that’s the visible part of the link. Note: If the link is to the current/same page, you can just leave the base URL empty, which will result in a relative url to the current page.

One of the points of user written functions is they help simplify your main application code by handling repeated tasks. So, the above code that builds the link would be something that would be useful if it was made into a function.

This function should accept three input parameters corresponding to the three ‘dynamic’ values in the code (I would name them $base_url, $id, and $label.) The code inside the function should produce a valid html link using those input parameters and return the link to the calling code. You would call this function inside a loop that’s building the links from your array of doctor information. The $label value that will be the display portion of the links would be something like the doctor’s name and/or address.

If you build the array of doctor information with the id value as the array index/key, you can just use isset() to find if the submitted id value exists, and can directly reference the entry for the corresponding doctor. There’s no need to do any array search (unless your assignment IS to use array_search()).


#17

I will post the full exercise and what I’ve done with php/html because posting just parts it’s just confusing in my opinion, the only problem some words are in Italian! I will translated it then post it


#18

first i had to create and array of doctors called “dentisti” with all the infos , print in a html table and make a function making the telephone number called “telefono” with a point between the third number and the fourth and print the number of the doctors in the array.
in this exercise i’ve to select a doctor of the list through a link and a new page will be opened just the infos about the selected doctor:

1)make a function who receive as parameter an ID and return a string given by the url with the ID as parameter
2)Add a link HTML on every doctor record , creating a new column who aim to the URL created by the function
3)clicking on the link a new page will be opened reading the URL ID
4)with the retrieved ID , search in the array who is the doctor and retrieve all the infos
5)all the infos will have to be printed in a new table with 2 coloumn “name” “info”

here’s what i’ve done will now, many words are in italian

DENTISTI
<?php

$nl="
";
static $dentisti;
//$dentisti=array(“nome”, “id”, “ragione_sociale”, “indirizzo”, “telefono” );
$dentisti=array();

$cellulare_formatted="";
$dentista1=array(“Luca”, “1234”, “Luca Rossi”, “viale monza 146”, “3335276282”);
$dentista2=array(“Mariella”, “6353”, “Mariella Verdi”, “via caviglia 3”, “3663638331”);
$dentista3=array(“Ludmilla”, “8372”, “Ludmilla Pini”, “viale lunigiana 5”, “3278423782”);
$dentista4=array(“Jacopo”, “3490”, “Jacopo Kadiri”, “via fermi 67”, “3459095479”);
$dentista5=array(“Giovanni”, “5739”, “Giovanni Abate”, “via torino 4”, “3844389380”);

$a= “nome”;
$b=“id”;
$c=“ragione_sociale”;
$d=“indirizzo”;
$e=“telefono”;
$f=“edit”;

function telefono ($telefono=null){
$prefisso= substr($telefono , 0 , 3);
$restantenum=substr($telefono , 3);
return $prefisso . “.” . $restantenum;
}

function getLink ($getlink){
$newlink=“http:www.prova.it/pagina.php?id=”;
return $newlink . $getlink ;
}

  // if (in_array("1234", $dentista1)) {
      //  echo "Luca Rossi - viale monza 146 - 3335276282";
    
    //if (in_array("6353", $dentista2)){
     //   echo "Mariella Verdi - via caviglia 3 - 3663638331";
   //}
   // if (in_array("8372", $dentista3)){
    //    echo "Ludmilla Pini - viale lunigiana 5 -  3278423782";
    //}
   //if (in_array("3490", $dentista4)){
    //    echo "Jacopo kadiri - via fermi 67 - 3459095479";
   // }
    //if (in_array("3490", $dentista5)){
     //  echo "Giovanni Abate - via torino 4 - 3844389380";
   //} 

?>

<?php

array_push($dentisti, $dentista1, $dentista2, $dentista3, $dentista4, $dentista5);

// var_dump($dentisti);
echo($nl);
foreach($dentisti as $dentista){
$cellulare_formatted=telefono($dentista[4]);
// var_dump($dentista);
?>

<?php } ?>
<?= $a; ?> <?= $b; ?> <?= $c; ?> <?= $d; ?> <?= $e; ?> <?= $f; ?>
<?= $dentista[0]; ?> <?= $dentista[1]; ?> <?= $dentista[2]; ?> <?= $dentista[3]; ?> <?= $cellulare_formatted; ?> info
<TABLE>

    <tr><td>Nome:</td><td>Informazioni:</td></tr>
    

        
        
    
    
    
    
</TABLE>
  
    <?php   

echo (" Totale Medici: " . count($dentista1));

?>