SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Website News

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Website News

    Ok, now i wanna start woking on a website news feature for my home page
    this feature will take infos (the website news) from the Database and will display them on the homepage

    now what i need help is with limiting the amount of news to 5 or so (so there wont be more than 5 lines of news in 1 page, i dont want a long list...), so the 5 newest infos will be viewed in page 1, and then rest will be viewed in page 2 , 3 and so on...(there will be buttons at the bottom to change between pages)

    When i say button to take to other page i dont mean page1.asp, page2.asp etc, i mean that only the Website news part will be changed.

    hope i made my point clear
    Thanks in advance,
    Ulthane

  2. #2
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What kind of database system are you using? Anyway I'm sure this can be accomplished with the SQL TOP statement. Check it out.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmm, read about it alittle bit at w3schools but i dont think thats excactly what im looking for..

    however i thought it might be abit difficult doing what i wanted to, so i think ill just make a db with 5 records in it, and when a new info is being added, it replaces the oldest one currently in the table, and get posted at the top of the website news on the site itself, but how can i get it done ?

    any ideas ?

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Surely you already know by now how to INSERT and DELETE records from a database table, and how to SELECT records and ORDER BY a column (e.g. DateInserted or ID)?

    In fact you don't even need to DELETE records - just have an ID field (AutoNumber format) and ...

    SELECT TOP 5 ID,Headline,Article FROM News ORDER BY ID DESC

    (As an example - syntax not checked).
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Lol yea, today at school excatly that idea came to me going to try it out, thanks

  6. #6
    SitePoint Member
    Join Date
    May 2006
    Location
    Phnom Penh, Cambodia
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ulthane View Post
    Ok, now i wanna start woking on a website news feature for my home page
    this feature will take infos (the website news) from the Database and will display them on the homepage

    now what i need help is with limiting the amount of news to 5 or so (so there wont be more than 5 lines of news in 1 page, i dont want a long list...), so the 5 newest infos will be viewed in page 1, and then rest will be viewed in page 2 , 3 and so on...(there will be buttons at the bottom to change between pages)

    When i say button to take to other page i dont mean page1.asp, page2.asp etc, i mean that only the Website news part will be changed.

    hope i made my point clear
    Thanks in advance,
    Ulthane
    Hi ! Please take a look at my code. You can include it in any page of your home page.


    Code:
    <%
    
    Dim rsJournal			'Database recordset holding the Journal items
    Dim rsCommentsCount		'Database recordset holding the count of comments for each Journal item
    Dim intRecordPositionPageNum	'Holds the number of the page the user is on
    Dim intRecordLoopCounter	'Loop counter to loop through each record in the recordset
    Dim intTotalNumJournalEntries	'Holds the number of Journal Items there are in the database
    Dim intTotalNumJournalPages	'Holds the number of pages the Journal Items cover
    Dim intLinkPageNum		'Holds the number of the other pages of Journal itmes to link to
    Dim blnGuest
    Dim strLoggedInUsername
    
    
    'If this is the first time the page is displayed then set the record position is set to page 1
    If Request.QueryString("p") = "" Then
    	intRecordPositionPageNum = 1
    
    'Else the page has been displayed before so the Journal item record postion is set to the Record Position number
    Else
    	intRecordPositionPageNum = CInt(Request.QueryString("p"))
    End If
    %>
    
    
    	 <%
    
    Dim Currpage, pageLen, lastNumber, PageRem, PageTen
    Dim connection, recordset, sSQL, sConnString, next10, prev10, P
    Dim RSPrevPage, RSNextPage, start 
    
    
    If IsEmpty(Request.Querystring("Page")) then
    CurrPage = 1
    Else
    CurrPage = Cint(Request.Querystring("Page"))
    End If
    
    'the two functions below return the next 10 and prev 10 page number
    Function getNext10(num)
    pageLen = len(num)
    If pageLen = 1 Then
    next10 = 10
    Else If pageLen>1 Then
    pageRem = 10
    pageTen = right(num, 1)
    next10 = num + pageRem - pageTen
    End If
    End If
    getNext10 = next10
    End Function
    
    Function getPrev10(num)
    pageLen = len(num)
    If pageLen = 1 then
    prev10 = 1
    Else If pageLen>1 then
    lastNumber = right(num, 1)
    prev10 = num - lastNumber - 10
    End If
    End If
    If prev10 = 0 then
    prev10 = 1
    End If
    getPrev10 = prev10
    End Function
    
    'create an instance of the ADO connection and recordset object
    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Recordset = Server.CreateObject("ADODB.Recordset")
    
    'define the connection string
    sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _ 
    "DBQ=" & Server.MapPath(".................. Your Database Part .mdb") & ";"
    
    'define our SQL variable
    sSQL="SELECT * FROM tblnews ORDER BY Date DESC" 
    
    'open an active connection
    Connection.Open sConnString
    
    'Next set the location of the recordset to the client side
    Recordset.CursorLocation = 3
    
    'Execute the SQL and return our recordset
    Recordset.open sSQL, sConnString
    ' pagesize is used to set the number of records that will be
    ' displayed on each page. For our purposes 10 records is what we want.
    Recordset.PageSize = 5
    %>
    
    
    <%
    'get the next 10 and prev 10 page number
    next10 = getNext10(CurrPage)
    prev10 = getPrev10(CurrPage)
    
    'If there are no records
    If Recordset.EOF Then
    Response.write "No records to display"
    
    Else
    'this moves the record pointer to the first record of the current page
    Recordset.AbsolutePage = CurrPage
    
    'the below loop will loop until all the records of the current page have been
    'displayed or it has reached the end of the recordset
    Do Until Recordset.AbsolutePage <> CurrPage OR Recordset.Eof
    
    'for our purposes our database has just 3 fields:
    'an 'ID' (autonumber field), 'SiteName' (textfield) and 'URL' (memofield)
    'you can change these according to your database and table fields
    
    response.write "<table width='550px' height='61'"" style='font: 13px arial;'>"
    response.write "<tr><td width='100px' height='40px' valign='top'><a href='/news.asp?id=" & Recordset ("ID") & "/'></a></td>"
    response.write "<td width='430px' height='35' valign='top'><b><a href='/news.asp?id" & Recordset ("ID") & "'>" & Recordset("title") & "</b></a> "</td>"
    response.write "<br /></tr></table>"
    Recordset.MoveNext
    Loop
    
    End If
    %>
    
    <%
    
    'the next 2 lines setup the page number for the "previous" and "next" links
    RSPrevPage = CurrPage -1
    RSNextPage = CurrPage + 1
    
    'find out the number of pages returned in the recordset
    'if the Next10 page number is greater than the recordset page count
    'then set Next10 to the recordset pagecount
    If Next10 > Recordset.PageCount Then
    Next10 = Recordset.PageCount
    End If
    
    'the variable start determines where to start the page number navigation
    ' i.e. 1, 10, 20, 30 and so on. 
    If prev10 = 1 AND next10 - 1 < 10 Then
    start = 1
    Else
    start = Next10 - 10
    If right(start, 1) > 0 Then
    start = replace(start, right(start, 1), "0")
    start = start + 10
    End If
    End If
    
    'This checks to make sure that there is more than one page of results
    If Recordset.PageCount > 1 Then
    
    'Work out whether to show the Previous 10 '<<' 
    If currpage > 1 Then
    response.write("<span class='pagelink'> <a href=""?page=" & Prev10 & """><<</a> </span>&nbsp;")
    End If
    
    'Work out whether to show the Previous link '<' 
    If NOT RSPrevPage = 0 then
    response.write("<span class='pagelink'> <a href=""?page=" & RSPrevPage & """><</a> </span>")
    End If
    
    'Loop through the page number navigation using P as our loopcounter variable 
    For P = start to Next10
    
    If NOT P = CurrPage then
    response.write("<span class='pagelink'> <a href=""?page=" & P & """>" & P & "</a></span> &nbsp;")
    Else
    
    'Don't hyperlink the current page number 
    response.write("<span class='paging'><b>" & P & " </b></span> &nbsp;")
    End If
    Next
    
    'this does the same as the "previous" link, but for the "next" link
    If NOT RSNextPage > Recordset.PageCount Then
    response.write("<span class='pagelink'> <a href=""?page=" & RSNextPage & """>></a></span><br><br> &nbsp;")
    End If
    
    'Work out whether to show the Next 10 '>>' 
    If NOT Next10 = Recordset.PageCount Then
    response.write(" &nbsp; <span class='pagelink'> <a href=""?page=" & Next10 & """>>></a> </span> &nbsp;")
    End If
    End If
    
    ' Close the recordset and connection object
    Recordset.Close 
    Set Recordset = Nothing
    Connection.Close
    Set Recordset =Nothing 
    %>

    Hope this can help you , get back to me, if you have any problem


Bookmarks

Posting Permissions

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