Solved

Call custom function in php and arrange data as attach file

Posted on 2016-08-01
7
43 Views
Last Modified: 2016-08-05
Hello;

I retrieve data with a query as a custom function as below:

QUERY

 <?php
		 
	 $query_artandculture = "select tbarticles.articleid,tbarticles.title,substr(tbarticles.articlebody,1,200) as description1,tbphotos.photofilename,tbarticles.deletitem ,tbarticlecategories.catid,tbcategories.parentid,tbarticles.mediastate,tbphotos.altdescription
	 
from  tbarticles 

INNER JOIN tbphotos on tbphotos.articleid = tbarticles.articleid 
INNER JOIN tbarticlecategories on tbarticlecategories.articleid =  tbarticles.articleid
INNER JOIN tbcategories ON tbcategories.catid = tbarticlecategories.catid


where tbarticles.deletitem='1'   AND (tbcategories.parentid ='1' or  tbcategories.parentid ='2' or tbcategories.parentid ='3')


	  ORDER BY tbarticles.articleid desc
LIMIT 8
";

$result_artandculture = mysql_query($query_artandculture);
?>

Open in new window


FUNCTION

function doArticle_image300_artandculture($articleid,$title,$description1,$photo,$parentid,$catid,$mediastate,$altdescription) {
{
    // CREATE A RETURN STRING IN HEREDOC NOTATION
      $tab = <<<EOD
    <div class="wrapper-for-datapakage-firstpage">
      <div class="articletitle"><a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title 
		   </strong></a></div>
      <div class="row2-main-datapakage"><a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
		   <img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/></div>
      <div class="row3-main-datapakage">$description1...</div><hr class='hr95'></div>
    </div>
EOD;

    return $tab;
}
		  
          }
          $tab = "";
}

Open in new window


I want that arrange each block of data (call function) as 2 columns and in each row 2 blocks .

Thanks
0
Comment
Question by:MOSTAGHASSI
  • 3
  • 2
  • 2
7 Comments
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41738364
I'm not sure I understand the question. But your function code contains some mismatched curly braces. Line 1 ends with a begin curly brace and then line 2 contains another begin curly brace. Then lines 15, 17, and 19 each begin with an end curly brace. So that section of code should throw an error when parsed.

The snippet of code you labeled query appears to perform a database query and store the results in the variable $result_artandculture. Are you asking how to get the results of your query into the function?

I want that arrange each block of data (call function) as 2 columns and in each row 2 blocks .
This sounds as if you want to separate each block of data into 2 columns and arrange two blocks side by side for 4 columns on each row. Is this correct? What are you referring to as a block of data? Is that one row in the query result? The HTML code in your function appears to divide the data into three divs which all are direct descendants of the same parent div. If you wish to arrange these divs side-by-side, you need to show us your CSS for these elements.
0
 

Author Comment

by:MOSTAGHASSI
ID: 41738494
Thanks,sorry for mistake the  braces,it has happen during copy past,and forget it the function work well,

Are you asking how to get the results of your query into the function?

No.

This sounds as if you want to separate each block of data into 2 columns and arrange two blocks side by side for 4 columns on each row. Is this correct?

Yes and no.
the arrangement are like:

block1      block2

block3      block4

block5      block6

block7      block8

What are you referring to as a block of data?

Data block is 'function' that  contain 3 div(1-for title with a link-2 for image for the same link-3 small description for article).
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41739736
I'm sorry. I still don't understand what you're asking for. Are you asking how to arrange your blocks of data in two columns? Are your blocks of data contained in a parent div with class wrapper-for-datapakage-firstpage?

If so, arranging the parent divs side by side in two columns is done with CSS, not PHP. This can be accomplished by wrapping all the wrapper-for-datapakage-firstpage divs in another div (if they aren't already) which is wide enough for two, and then floating all the wrapper-for-datapakage-firstpage divs.

Executing your doArticle_image300_artandculture function eight times would generating the eight blocks of data within the two-columns div below.
<div class="two_columns">
	<!-- Block 1 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 2 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 3 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 4 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 5 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 6 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 7 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 8 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
</div>

Open in new window

The CSS to place them side by side would look something like
div.two_columns { width: 600px; height: auto; overflow: hidden; }
div.wrapper-for-datapakage-firstpage { width: 300px; margin: 0; padding: 0; float: left; }

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41742621
Couple of things.  First and most important: Get off MySQL.  Choose MySQLi  or choose PDO.  Here's the background telling why I recommend you get off MySQL as soon as possible, along with code examples showing how to make the change.  In my experience, object-oriented MySQLi  is the easiest conversion from MySQL.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Second, you might want to take this question under consideration as a Model-View-Controller design pattern that uses a template, probably a HEREDOC, to build the HTML view of this data.  To do this, first write your HTML template, using PHP variables in the places that make sense, something like this:
<div><span>$block1</span><span>$block2</span></div>
<div><span>$block3</span><span>$block4</span></div>
<!-- etc... -->

Open in new window

Next, write your PHP code to assign the values to $block1, $block2, etc.  Test to verify that the correct data is getting put into the correct PHP variables and that these variables are getting injected into the HTML template.

Finally, add appropriate selector attributes (id, class) to the HTML tags, and write your CSS (and/or JavaScript) to cause the styling you need.

Information on PHP HEREDOC notation is available here.  Heed, but do not be put off by their "over-the-top" warning.  This is a really helpful feature of the language!
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
0
 

Author Comment

by:MOSTAGHASSI
ID: 41743203
Thanks Ray,specially for your great article, it seams that we must be ready for everyday upgrading,for this question i need your help,for convert this part of my old codes(works well) from 'table' to 'div' :

<?php
$result_artandculture = mysql_query($query_artandculture);

if ( $result_artandculture) {
	
	$tab = "<table width = 640  valign=top border=0 cellspacing=1 bordercolor=#ffffff bgcolor=#FFFFFF style=border: 1px solid #0066ff>\n";
	
	      $i = 1;
     for ( $i=1 ; $i<=4; $i++){
    $rw = @mysql_fetch_assoc( $result_artandculture);
     $tab .= "<tr>\n";
     $tab .= "<td   align=center bordercolor=#ffffff >" .doArticle_image300_artandculture( $rw['articleid'], $rw['title'],    $rw['description1'], $rw['photofilename'], $rw['parentid'], $rw['catid'],$rw['mediastate'],$rw['altdescription']) . "</td>\n"; 
	 
	 
	 if ( $rw = @mysql_fetch_assoc($result_artandculture ) )
	 $tab .= "<td   align=center bordercolor=#ffffff >" .doArticle_image300_artandculture( $rw['articleid'], $rw['title'], $rw['description1'], $rw['photofilename'], $rw['parentid'], $rw['catid'],$rw['mediastate'],$rw['altdescription']) . "</td>\n";		
	 
	 $tab .= "</tr>\n";
     }
	 $tab .= "</table>\n"  ; 
	} 
    echo   "$tab"; 
     ?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41743278
This expert suggested creating a Gigs project.
This is not really a question with an answer -- it's more of a requirement for application development.  You might do well to try E-E Gigs.
0
 

Author Comment

by:MOSTAGHASSI
ID: 41744375
Please let me know that how much is normal for this very small project?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now