include with parameter?

Asked

Viewed 2,149 times

2

There is include with parameter?

I have a page .php containing the following code:

<ul class="nav nav-tabs">
  <li class="active"><a data-toggle="tab" href="#segunda">Segunda-Feira</a></li>
  <li><a data-toggle="tab" href="#terca">Ter&ccedil;a-Feira</a></li>
  <li><a data-toggle="tab" href="#quarta">Quarta-Feira</a></li>
  <li><a data-toggle="tab" href="quinta">Quinta-feira</a></li>
  <li><a data-toggle="tab" href="#sexta">Sexta-feira</a></li>
  <li><a data-toggle="tab" href="#sabado">S&aacute;bado</a></li>
</ul>

The Nav Nav-tabs form 6 tabs and in each ABA I show the times of FULANINHO.

  • In the ABA MONDAY I show the Monday schedules of FULANINHO
  • In the TAB TUESDAY I show the Tuesday schedules of FULANINHO
  • And so on and so forth.

When I started making the code I saw that it would be a repetition of everything, where I would only change the day of the week and the name of FULANINHO that comes from the previous page via POST.

So I thought of something like include('segunda.php',$id_fulaninho). That’s possible?

If not, it would have some shape that I still don’t know (I’m new to the php) to reduce this absurd amount of code repeated?

  • make the include and on the set include the time and the name of the John or the time and the name of Ciclaninho etc... according to what comes from the post. In order to help it would be good to post the code of the previous page of the post and include and how to get the data of the fulaninho, etc..

  • Why don’t you encapsulate the code in a function that receives the parameters and instead of giving the include to display the HTML, you include the file with the function and call where you want to display the result.

  • Good morning Leo Caracciolo and Anderson Carlos... Thank you for your attention. As I said I am new to the dialect "peagapes";) I will study your words carefully and try to do what you have indicated :)

2 answers

3


There is no include with Paramento, but, I could notice in his question that it is easy this, a example to illustrate:

Create the file that will be including in the other:

File name: _id.php

<?php

    echo $id;

then create the file with this include:

File name: vid.php

<?php

    $id = 100;
    include('_id.php');

thus the file _id.php has access to variable $id and its value can be used peacefully, equal and similar to what you intend in your flaps.

It is worth remembering that the include can be used with the protocol HTTP and then passing vestments on the url:

<?php
    include('http://www.example.com/s.php?id=100');

but if the file is part of your project (on the same file systems), does not accept parameters.

References

  • 3

    @Andersoncarloswoss has realized that the only plausible solution are his, It is commented in the post of everyone is becoming a boring guy on the network.

  • 1

    As you said opinion... Your tip also think bad for several factors, but, it’s not the case. If you have a better answer post and are graced with the votes and acceptance is how the majority behaves, now all the time you keep saying do this or that is becoming boring the situation. @Andersoncarloswoss thank you.

  • Good morning Virgilio Novic.. Thank you for your attention. I also thank you for sharing your knowledge with everyone. I will carefully read all the answers with A LOT of attention . I know that the world is round and there are several ways to get to the same place. I’m sure your answer works. But I have to study and understand. As I’ve commented a few times in this post I’m new in this business of php :)

  • Of course @Ricardom.Souza, this solution was given in the context of your question, that is, it will depend and much as is the general context of your application, maybe you can use other techniques and/ or other forms, the answer in question was the first I saw through what you questioned, but, you may ask if you have any pertinent questions, I am here and you may ask ... Thank you ... also

  • Good afternoon @Virgilio. Dude, I did like you said and it was 1000%:). In reality what happened was that I had until then thought that the variables that came from the previous screen through the post could NOT be read inside a INCLUDE file in the middle of the php page. Another thing I was THINKING WRONG and that each INCLUDE file within the <html></html> page would NOT communicate with another. Like if I have include(AA.php) and in this file I create the variable $dia, I thought that in the include(BB.php) file that is 20 lines below include AA.php the BB.php file would NOT read the variable $dia.Again THANK YOU

  • OK @Ricardom.Souza was in this aspect that I wanted you to arrive and understand, with this you can propose better things in your code, but with this prior knowledge. If that answer served you and you want to mark as answer to your question accepted as answer!

  • Face at the time I read.. I already scored.. I say) I already gave the vote ... but mark it as an answer.. I REALLY DIDN’T NOTICE it until now :)... I will mark yours and Antonio Alexandre’s. I also tested the way he mentioned and it worked beauty. Only the way you explained it is so much more practical right now. Another thing I just learned is what the ARROBA is for in front of the name :). Poxa.. just to put an answer to the question... :( Marquei a Sua. I owe it to Antonio Alexandre :)

  • 1

    Just to complement :) I didn’t even know I had CHAT here. A message appeared saying to avoid extensive discussions and if I would like to move to chat.. I didn’t move them to the chat because I think they are relevant information and I believe that in the chat such information would be restricted to both. Hugs :)

Show 3 more comments

2

I suggest the following solution:

Create a function to receive both parameters and return the desired html output.

Ex:

Function getHorarios.php

<?php

function getHorarios($dia_semana, $id_pessoa)
{
    $html_output = '';

    /*
    // .= concatena strings
    $html_output.= '10:00 - Alguma coisa. <br>';
    $html_output.= '12:00 - Outra  coisa. <br>';
    */

    return $html_output;
}

ajaxHorarios.php - Example function call:

<?php

    require('getHorarios.php');

    if( !empty($_POST["dia_semana"]) && !empty($_POST["id_pessoa"] )
    {
        $dia_semana = $_POST["dia_semana"];
        $id_pessoa  = $_POST["id_pessoa"];

        echo getHorarios($dia_semana, $id_pessoa);
    }
    else
    {
        echo 'Erro';
    }

For calls to ajax: how to pass data to a php function through ajax url?

  • @Andersoncarloswoss good tip. I already knew and I used the function Empty a lot, but I didn’t notice that it could be used instead of isset. Thank you. I changed the example.

  • Good morning Antonio Alexandre.. Thank you for sharing your answer :). I understood what the GETHORAIOS function does. and Ajaxhorarios.php does too.. The thing of calling the ajax function I even did and it was cool :). Now.. all together the way you suggested had not yet seen this way by the lack of experience. Thank you very much.. I’ll spend today studying and understanding your example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.