how to echo the first record and second record in the same loop?

Discussion in 'PHP' started by xbat, Oct 3, 2014.

Thread Status:
Not open for further replies.
  1. #1
    how to echo the first record and second record in the same loop?

    $something->query Select * from table where soandso = this thing

    $something2->query Select * from table where soandso = this thing +1

    while $row -> $something {
    while $row2 -> $something2 {
    $row ['id'];
    $row2 ['id'];
    }
    }


    will output something like this

    row = record1

    row2 = record1, record2 etc...

    I only need it to display one record..

    Any suggestions would be appreciated.
     
    Solved! View solution.
    xbat, Oct 3, 2014 IP
  2. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #2
    here this may help more..


    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $user = 'root';
    $pass = '';

    $dbh = new PDO('mysql:host=localhost;dbname=testonly', $user, $pass);


    $findtable= $dbh->query("SELECT * FROM table1");
    $findtable->setFetchMode(PDO::FETCH_ASSOC);
    $findtable2= $dbh->query("SELECT * FROM table1 ORDER BY id +1");
    $findtable2->setFetchMode(PDO::FETCH_ASSOC);
    while($showfindtable2 = $findtable2->fetch()){
    while($showfindtable = $findtable->fetch()){


    echo $showfindtable['range1']; echo '<BR>';

    echo't2 >'. $showfindtable2['range1']; echo '<BR>';
    }
    }





    CREATE DATABASE IF NOT EXISTS `testonly` /*!40100 DEFAULT CHARACTER SET utf8 */;
    USE `testonly`;
    -- MySQL dump 10.13 Distrib 5.6.13, for Linux (x86_64)
    --
    -- Host: 127.0.0.1 Database: testonly
    -- ------------------------------------------------------
    -- Server version 5.1.73

    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

    --
    -- Table structure for table `table1`
    --

    DROP TABLE IF EXISTS `table1`;
    /*!40101 SET @saved_cs_client = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `table1` (
    `id` int(11) NOT NULL,
    `range1` varchar(45) DEFAULT NULL,
    `range2` varchar(45) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;

    --
    -- Dumping data for table `table1`
    --

    LOCK TABLES `table1` WRITE;
    /*!40000 ALTER TABLE `table1` DISABLE KEYS */;
    INSERT INTO `table1` VALUES (0,'4','8'),(1,'8','8'),(2,'9','20'),(3,'21','25'),(4,'26','30');
    /*!40000 ALTER TABLE `table1` ENABLE KEYS */;
    UNLOCK TABLES;
    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

    -- Dump completed on 2014-10-03 21:34:41



    results

    4
    t2 >4
    8
    t2 >4
    9
    t2 >4
    21
    t2 >4
    26
    t2 >4


    What my results should be

    4
    t2 >8
    8
    t2 >9
    9
    t2 >21
    21
    t2 >26
    26
    t2 >nothing
     
    Last edited: Oct 3, 2014
    xbat, Oct 3, 2014 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #3
    why aren't you just using limit 2?
    $sql = "Select * from `table` where `soandso` = 'this thing' limit 2";
    PHP:
     
    sarahk, Oct 3, 2014 IP
  4. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #4
    If I do limit 2 on $findtable2= $dbh->query("SELECT * FROM table1 ORDER BY id +1 limit 2");

    it gives me this..


    4
    t2 >4
    8
    t2 >4
    9
    t2 >4
    21
    t2 >4
    26
    t2 >4


    If it helps any I put the code above.. I am sure I am missing something.. there has to be a better way of doing this then what I already am. I appreciate the try.
     
    Last edited: Oct 3, 2014
    xbat, Oct 3, 2014 IP
  5. #5
    gotcha
    
    $dbh = new PDO('mysql:host=localhost;dbname=testonly', $user, $pass);
    
    $findtable= $dbh->query("SELECT * FROM `table1` order by `range1`");
    $findtable->setFetchMode(PDO::FETCH_ASSOC);
    $prevRange1 = '';
    
    while($showfindtable = $findtable->fetch()){
        if (!empty($prevRange1)) {
            echo "{$prevRange1}<BR>
            t2 > {$showfindtable['range1']}<BR>";
        }
        $prevRange1 = $showfindtable['range1'];
    }
    // now echo out the last one
    echo "{$prev}<BR>
            t2 > nothing<BR>";
    PHP:
    but I'd have probably fetched it as an array and then just looped through with a for($i = 0; $i < count($result); $i++) and referenced the next range number using the $i - cleaner than saving the previous range.
     
    sarahk, Oct 3, 2014 IP
  6. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #6
    you are the man! Or woman.. But you are it!! Thank you
     
    xbat, Oct 3, 2014 IP
Thread Status:
Not open for further replies.