Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Is it possible to output database rows and values from an array simultaneously

    When outputting database rows is it possible to also output values from an array simultaneously
    I know I have six database rows to which I would like to add one of six different classes

    $icon_classes = array('icon1', 'icon2', 'icon3', 'icon4', 'icon5', 'icon6');

    Somehow add the class names to each row
    class="contact-phone-icon icon1" class="contact-phone-icon icon2" ETC

    PHP Code:
    $results $connection->query("SELECT message FROM contact");

    while(
    $row $results->fetch_assoc()){   $rows $row["message"]; 
     
     echo 
    '<div class="contact-inner-wrapper"><div class="contact-phone-icon icon1">

    <i class="fas fa-phone fa-2x"></i></div><div class="contact-phone-text">

    <p>' 
    $rows '</p></div></div>';   }

    $mysqli->close();

    $connection->close(); 

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,257
    Thanks
    3
    Thanked 553 Times in 538 Posts
    Just use a variable that you increment to provide the numerical part of the class name.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,329
    Thanks
    4
    Thanked 480 Times in 468 Posts
    Are you sure you even need a class? You've already got far, far, FAR too many of them just like all the DIV and SPAN for nothing. (and variable for nothing).

    I'd have to see a sample of the content and what you're doing with it, but I suspect this is a job for nth-child in the stylesheet and not anything the server-side code should be doing.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  4. #4
    New Coder
    Join Date
    Jun 2012
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi All,
    Thanks for the advice, I will try to keep my code as light as possible.

    Also thanks for the tip about incrementing the variable.

    From a codding perspective if I also wanted to include the strings representing the icons,
    is there a similar technique for doing this?

    $icons = array('fas fa-phone fa-2x', 'fas fa-mobile-alt fa-2x', 'fas fa-at fa-2x', 'fas fa-globe fa-2x');

    class="fas fa-phone fa-2x" class="fas fa-mobile-alt fa-2x" ETC

    PHP Code:

    $class_num 
    1;

    while(
    $row $results->fetch_assoc()){   $rows $row["message"]; 
     
     echo 
    '<div><div class="contact-phone-icon icon'.$class_num.'">

    <i class="fas fa-phone fa-2x"></i></div><div class="contact-phone-text">

    <p>' 
    $rows '</p></div></div>';  

     
    $class_num++;
     }

    $mysqli->close();

    $connection->close(); 


 

Tags for this Thread

Posting Permissions

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