Need to compare numbers in a table

Discussion in 'PHP' started by rhoula, Jan 14, 2013.

  1. #1
    I really don't know how to phrase this, but I think you will get the point if you are familiar with php and mysql.

    I have a small php script that makes a search and displays information about my websites' Alexa rank. I'm trying to make it to where after a search is done I would have an arrow at the end of each row indicating if the rank went up went down or stayed the same.

    Please see images of the search table I'm getting now, and see how I want it to look like in the bottom.

    Here is the link where you can test the script:

    http://oklahomies.com/Alexa/Script/search.php

    and here is the code I have so far:

    
       <?php
      if(isset($_POST['submit'])){
      if(isset($_GET['go'])){
      if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
      $name=$_POST['name'];
    
      //connect  to the database
      $db=mysql_connect  ("localhost", "user",  "password") or die ('I cannot connect to the database  because: ' . mysql_error());
    
      //-select  the database to use
      $mydb=mysql_select_db("db427107316");
    
      //-query  the database table
      $sql="SELECT  ID, date, domain, links, alexa FROM alexa WHERE domain LIKE '%" . $name .  "%' OR links LIKE '%" . $name ."%'";
    
      //-run  the query against the mysql query function
      $result=mysql_query($sql);
    {
    	echo '<table border=1 width=600><tr><td><b><center>Date</center></b></td><td><b><center>Domain</center></b></td><td><b><center>Alexa</center></b></td><td><b><center>Links<center></b></td></tr>';
    
    
      //-create  while loop and loop through result set
      while($row=mysql_fetch_array($result)){
              $domain  =$row['domain'];
              $links=$row['links'];
    		  $alexa=$row['alexa'];
    		  $date=$row['date'];
              $ID=$row['ID'];
    
      //-display the result of the array
      echo "<tr><td><center>"   .$date . "<center></td><td>&nbsp;&nbsp;"   .$domain . "</td><td align=right>" . $alexa .  "&nbsp;&nbsp;</td><td align=right>" . $links . "&nbsp;&nbsp;</td></tr>";
    }
      echo "</table>";
    
      }
      }}
      else{
      echo  "<p>Please enter a search query</p>";
      }
      }
    ?>
    
    PHP:
    http://oklahomies.com/Alexa/Script/search.php

    Please use: Checkthisup.com as an example

    search_1.png
    search_2.png

    This is the link to the image files:

    http://oklahomies.com/Alexa/Script/images/up.png
    http://oklahomies.com/Alexa/Script/images/down.png
    http://oklahomies.com/Alexa/Script/images/same.png
    http://oklahomies.com/Alexa/Script/images/next.png

    Thank you
     
    rhoula, Jan 14, 2013 IP
  2. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #2
    What I need is just a function that will echo the "arrow kind" depending on the number bellow.

    Please someone help me :(
     
    rhoula, Jan 15, 2013 IP
  3. cochisetm

    cochisetm Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #3
    You should put your db results into an array. Something like:

    $array_results = array();

    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)){
    $array_results[] = array(
    'domain' => $row['domain'],
    'alexa' => $row['alexa']
    etc.....
    );

    You can then try something like (untested):
    foreach($array_results as $k=>$v) {
    if($k == 0) {
    $arrow = '';
    } else {
    $arrow = ($v['alexa'] > $array_results[$k - 1]['alexa']) ? 'up' : 'down';
    }
    echo 'Domain = '. $v['domain']. ' Arrow: '. $arrow. '<br>';
    }

    There are of course many other ways to do the same thing, but this should be enough to get you started.
     
    cochisetm, Jan 15, 2013 IP
  4. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #4
    I tried to get that to work. Cannot get it working to save my life.

    Please someone help me. I have to get this done asap.

    Thank you in advance
     
    rhoula, Jan 15, 2013 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    post the codes from search_now.php, its probable easier to add if its up or down from there, rather then trying to compare
     
    MyVodaFone, Jan 15, 2013 IP
  6. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #6
    This the search_now.php code:

    
    <table border=0 bgcolor="FFFFFF" width=800 height=100>
    
    <tr>
    <a href="search.php">Do another search </a>
    
    
    <td width="300" height="100"><font class="content">
    
        <?php
      if(isset($_POST['submit'])){
      if(isset($_GET['go'])){
      if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
      $name=$_POST['name'];
    
      //connect  to the database
      $db=mysql_connect  ("localhost", "user",  "Password") or die ('I cannot connect to the database  because: ' . mysql_error());
    
      //-select  the database to use
      $mydb=mysql_select_db("db427107316");
    
      //-query  the database table
      $sql="SELECT  ID, date, domain, links, alexa FROM alexa WHERE domain LIKE '%" . $name .  "%' OR links LIKE '%" . $name ."%'";
    
      //-run  the query against the mysql query function
      $result=mysql_query($sql);
    {
    	echo '<table border=1 width=600><tr><td><b><center>Date</center></b></td><td><b><center>Domain</center></b></td><td><b><center>Alexa</center></b></td><td><b><center>Links<center></b></td></tr>';
    
    
      //-create  while loop and loop through result set
      while($row=mysql_fetch_array($result)){
              $domain  =$row['domain'];
              $links=$row['links'];
    		  $alexa=$row['alexa'];
    		  $date=$row['date'];
              $ID=$row['ID'];
    
      //-display the result of the array
      echo "<tr><td><center>"   .$date . "<center></td><td>&nbsp;&nbsp;"   .$domain . "</td><td align=right>" . $alexa .  "&nbsp;&nbsp;</td><td align=right>" . $links . "&nbsp;&nbsp;</td></tr>";
    }
      echo "</table>";
    
      }
      }}
      else{
      echo  "<p>Please enter a search query</p>";
      }
      }
    ?>
    
    <!--
    {
    	echo '<table border=1 width=600><tr><td><b><center>#</center></b></td><td><b><center>Date</center></b></td><td><b><center>Domain</center></b></td><td><b><center>Alexa</center></b></td><td><b><center>Links<center></b></td></tr>';
    	while($row = mysql_fetch_array($rows))
    	{
    		echo "<tr><td><center>".$row['id']."</center></td><td><center>".$row['date']."</center></td><td>&nbsp;&nbsp;".$row['domain']."</td><td align=right>".number_format($row['alexa'])."&nbsp;&nbsp;</td><td align=right>".number_format($row['links'])."&nbsp;&nbsp;</td></tr>";
    	}
    	echo '</table>';
    	
    	
    }else
    {
    	echo "sorry no records found";
    }
    -->
    
    
    </td>
    
    </tr>
    </table>
    
    PHP:

    Thank you
     
    rhoula, Jan 16, 2013 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    Sorry I meant the script that gets the information from Alexa
     
    MyVodaFone, Jan 16, 2013 IP
  8. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #8
    I finally got it to work, but when the alexa rank stay the same, it does not show the right image. it either shows the "up image" or the "down image". Can you please help.

    Can you please check it out here: http://oklahomies.com/Alexa/Script/search111.php


    Thank you
     
    rhoula, Jan 16, 2013 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    I'm not seeing it here, but where are you storing the old rank vs. the latest? If you don't have the number from the previous update stored, how can you know if it went up or down?

    * SIDE NOTE * -- what's with the CENTER tags doing align's job, TD, CENTER and B doing TH's, job, non-breaking space's doing padding's job, single TD tables for nothing, invalid placement of an anchor inside a TR, mismatched/broken widths (600 inside a 300?), mere presence of tags like FONT and CENTER that have no business in HTML written after 1998, and in general decade and a half out of date HTML methodology? Just saying...
     
    deathshadow, Jan 16, 2013 IP
  10. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #10

    Thank you so much for the great reply. The script is still under construction. I'm fixing to add so many more new features to it. Once I'm done with the script I will definitely add more cosmetics to it and fix all what needs to be fixed, css and some good looking graphics.

    Maybe once I'm ready I will ask for your help since I see that you are in expert in finding what is wrong with websites (your signature)

    Have a wonderful day :)
     
    rhoula, Jan 17, 2013 IP
  11. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #11
    rhoula, Jan 17, 2013 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #12
    Still didn't answer the important question though -- where are you storing the previous value and/or where it went up or down? Kinda hard to display that information when you don't appear to be storing it.
     
    deathshadow, Jan 17, 2013 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #13
    Oh wait -- nevermind, I see it... that's simple -- store with an IF, loop the next, move the next.

    I rewrote this as PDO since if this is new code you shouldn't be using the mysql_ statements -- hence the big red box on every page of php.net to that effect.. Also means no need to worry about sanitization as it would be handled for you.

    You need to use a LOT less variables in there -- avoid copying values when you don't need to.

    I would put the row echo in it's own function -- that way it can be called from two different spots:
    
    function echoAlexaRow($data,$movement) {
    	$imageName = is_numeric($movement) ? (
    		$movement<0 ? 'down' : (
    			$movement == 0 : 'equal' : 'up'
    		)
    	) : $movement;
    	echo '
    		<tr>
    			<th scope="row">',$data['date'],'</th>
    			<td>',$data['domain'],'</td>
    			<td>',$data['alexa'],'</td>
    			<td>',$data['links'],'</td>
    			<td><img src="images/',$imageName,'.png" alt="',$imageName,'" /></td>
    		</tr>';
    }
    
    Code (markup):
    The logic flow then becomes a fairly simple:

    
    if ($showRow = $statement->fetch()) {
    	while ($nextRow = $statement->fetch()) {
    		echoAlexaRow($showRow,$showRow['alexa'] - $nextRow['alexa']);
    		$showRow = $nextRow;
    	}
    	echoAlexaRow($showRow,'next');
    }
    
    Code (markup):
    See how that works -- get the row we want to show, then the while will grab the next row so we can compare. Using subtract lets us easily pass the value to be processed by the function. Afterwards we copy that next row to our row to be shown, repeat if possible, output the last one as 'next' otherwise. Sending the string 'next' lets us have that third state while keeping the logic simple. (and let you send other values if need be!)

    A full rewrite (using PDO) would go something like this:
    
    <div class="alexaSearch">
    
    	<a href="search.php">Do another search </a>
    
    <?php
    
    function echoAlexaRow($data,$movement) {
    	$imageName = is_numeric($movement) ?	(
    		$movement<0 ? 'down' : (
    			$movement == 0 : 'equal' : 'up'
    		)
    	) : $movement;
    	echo '
    					<tr>
    						<th scope="row">',$data['date'],'</th>
    						<td>',$data['domain'],'</td>
    						<td>',$data['alexa'],'</td>
    						<td>',$data['links'],'</td>
    						<td><img src="images/',$imageName,'.png" alt="',$imageName,'" /></td>
    					</tr>';
    }
    
    if (
    	isset($_POST['submit']) &&
    	isset($_GET['go']) &&
    	preg_match("/^[  a-zA-Z]+/", $_POST['name'])
    ) {
    
    	$db = new PDO(
    		'mysql:host=localhost;dbname=db427107316',
    		'user',
    		'password'
    	);
    	
    	try {
    		$statement = $db->prepare('
    			SELECT
    				ID, date, domain, links, alexa
    			WHERE
    				domain like :name
    			OR
    				links like :name
    		');
    		
    		$statement->execute(array(
    			':name' => $_POST['name']
    		));
    		
    		if ($showRow = $statement->fetch()) {
    			echo '
    			<table class="alexaResults">
    				<caption>
    					Alexa results for ',htmlspecialchars($_POST['name']),'
    				</caption>
    				<thead>
    					<tr>
    						<th scope="col">Date</th>
    						<th scope="col">Domain</th>
    						<th scope="col">Alexa</th>
    						<th scope="col">Links</th>
    						<th scope="col">Up/Down</th>
    					</tr>
    				</thead>
    				<tbody>';
    				
    			while ($nextRow = $statement->fetch()) {
    				echoAlexaRow($showRow,$showRow['alexa'] - $nextRow['alexa']);
    				$showRow = $nextRow;
    			}
    			echoAlexaRow($showRow,'next');
    			
    			echo '
    				</tbody>
    			</table>';
    			
    		} else {
    			echo '
    			<p class="error">
    				No results found
    			</p>';
    		}
    
    	} catch (PDOException $e) {
    		echo '<pre>',$e->getMessage,'</pre>';
    	}
    
    } else {
    	echo '
    			<p>
    				Please enter a search query.
    			</p>';
    }
    
    ?>
    <!-- .alexaSearch --></div>
    
    Code (markup):
    (untested code, might be a typo or two)

    Sorry there, I think your lack of formatting and mix of coding styles was making me not see what you were trying to do.
     
    Last edited: Jan 17, 2013
    deathshadow, Jan 17, 2013 IP
  14. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #14
    For some reason the code is not working. This is what I get when I run it:

    Thank you
     
    rhoula, Jan 17, 2013 IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #15
    Typo. Line ten should read:
    $movement == 0 ? 'equal' : 'up'

    As I said, might be a typo or two. I REALLY shouldn't try to write code on the laptop.
     
    deathshadow, Jan 17, 2013 IP
  16. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #16
    This is what I'm getting now:

    Thank you
     
    rhoula, Jan 17, 2013 IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #17
    That doesn't even make sense since there should be no { on line 37.

    Alright, I'm gonna make a quick local DB just to test the code once I'm awake, breakfast in me, and at the workstation.
     
    deathshadow, Jan 18, 2013 IP