Pick up information from one foreach with another foreach

Asked

Viewed 596 times

0

I have a mysql search that brings me the following array when using a foreach, I have another sql search that brings me all the fields of a table that in case are the same field names that are below usr_tbl_ssid... as I do to display only the value of contacts_form_name, Whatsapp and on the side the names of the fields and at the same time hide the empty fields?

Array
(
    [usr_tbl_ssid] => 
    [0] => 
    [contacts_form_name] => Fye Flourigh
    [1] => Fye Flourigh
    [whatsapp] => +5521999
    [2] => +5521888
    [email] => 
    [3] => 
    [site] => 
    [4] => 
    [facebook] => 
    [5] => 
    [messenger] => 
    [6] => 
)

My code so far is like this

$statement = $conect -> prepare("SHOW COLUMNS FROM $usr_");
$statement -> execute();
$user_table_columns = $statement -> fetchAll();
foreach($user_table_columns as $user_columns => $columns) {
    $array_columns[] = $columns["Field"]; }

$statement = $conect -> prepare("SELECT * FROM $usr_ WHERE contacts_form_name = :contacts_form_name LIMIT 1");
$statement -> bindValue(":contacts_form_name", $_POST['search_content']);
$statement -> execute();
$_SESSION["search_send"] = $statement -> fetchAll();
foreach($_SESSION["search_send"] as $contacts_form => $search_data_content) {
    $array_data[] = $search_data_content; }

And I show them in a totally ugly way, like this

<?php foreach($array_columns as $index => $columns_content) { ?>
    <span><?php print_r( $columns_content ); ?></span>
<?php } ?>
<?php foreach($array_data as $index => $data_content) { ?>
    <span><?php print_r( $data_content ); ?></span>
<?php } ?>

How do I get only the fields with values and their values next to them

example

Contact name (contacts_form_name) Fye flourigh

Note: I can’t limit which fields I will see in mysql search because not six which fields each user created, so I have to run a show Columns.

1 answer

1


First of all, forgive me if my answer is not what you want, because I couldn’t quite understand your question.

for($i=0;$i < count($data_content);$i++) { // Faz um loop para pegar todos os elemento da array $data_content.
if($data_content[$i] != null) { // Se o valor do elemento da array for diferente de vazio ele continua
  if($i == 1) { // Se o valor do elemento da array for um(contacts_form_name)
    echo "Nome do contato:" . $data_content[$i] . "\n";
  }
  if($i == 2) { // Se o valor do elemento da array ser dois(whatsapp)
    echo "Whatsapp do contato:" . $data_content[$i] . "\n";
  }
  if($i == 3) { // Se o valor do elemento da array ser três(email)
    echo "Email do contato:" . $data_content[$i] . "\n";
  }
  if($i == 4) { // Se o valor do elemento da array ser quatro(site)
    echo "Site do contato:" . $data_content[$i] . "\n";
  }
  if($i == 5) { // Se o valor do elemento da array ser cinco(facebook)
    echo "Facebook do contato:" . $data_content[$i] . "\n";
  }
  if($i == 6) { // Se o valor do elemento da array ser igual a seis(messenger)
    echo "Messenger do contato:" . $data_content[$i] . "\n";
  }
}
}

I do not know if it is allowed to post links from external emulators, if it is not someone please let me know, but here is an example http://ideone.com/kj9fu1.

  • the first part I think helps a lot in what I need, I will test as soon as possible, already the parts below do not know if it will help because the problem is, I do not know what are the fields that the user created, Whatsapp, Messenger and email is just example, each database has the fields that the user determines, in this case, where are the names of the field that you put i = n• this would have to be dynamic, but can by external emulators yes.

  • then, looking at the emulator, this is exactly what I wanted for the first part, this was the one I was not able to elaborate, but, as it would be for an infinite number, instead of putting 1...6 as it would be to be infinite and take the name of array1, in array1 are the names of the fields that the user wrote and in the array2 that you did are the values.

  • 1

    @Sorry to keep you waiting. But, I took a look again at your question and I think now I understand (I think you get very flustered when you type, before "Press Enter", reads your own text, because it will make it easier for those who answer). If your question was what you wanted to show for example: contacts_form_name : Fye Flourigh was only to use the element if, take "id" from the elements of the variable $columns_content and synchronize with the ID’s $data_content. Example: echo $columns_content[$i] . " : " . $data_content[$i] . "\n";

  • The answer link above on Ideone

  • Its code is very good, but I am in extreme difficulty, my array does not look beautiful like yours, it is all full of information, I think multidimensional, as I do to leave the array of this example http://ideone.com/FrHa2K like yours?

  • one of the arrays I got, but Columns which is very complex is very difficult

  • @Nobih got it thanks to another question I asked, it worked well when I joined yours with his [link = http://answall.com/questions/125271/diminur-um-array-multidimencional/125283#125283]decrease-an-array-multidimencional[/link]

Show 2 more comments

Browser other questions tagged

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